我在哪里为Android 5.0中的工具栏小部件定义XML?

时间:2014-11-03 05:20:10

标签: android android-layout android-5.0-lollipop android-toolbar

好的,我现在已经经历了几个StackOverflow帖子,但我仍然对我的工具栏的xml的位置感到困惑。

<android.support.v7.widget.Toolbar
android:id=”@+id/my_awesome_toolbar”
android:layout_height=”wrap_content”
android:layout_width=”match_parent”
android:background=”@styles/colorPrimary” />

是否会进入我的/layout/activity_main.xml

2 个答案:

答案 0 :(得分:26)

工具栏是在应用布局中使用的Action栏的概括,现在回答你的问题有两种做法:

不良做法:

不好的做法是在每个布局中定义工具栏。

标准做法:

标准做法是定义布局并在基本活动中引用它。您只需要将此工具栏布局包含在您想要的任何布局中(使用<include>),并在任何活动中扩展已定义的基本活动。

此标准练习将帮助您为工具栏保留单个代码库,并节省您每次定义工具栏的时间。

示例:Google I / O 2014安卓应用

toolbar_actionbar_with_headerbar.xml

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:iosched="http://schemas.android.com/apk/res-auto"
    style="@style/HeaderBar"
    iosched:theme="@style/ActionBarThemeOverlay"
    iosched:popupTheme="@style/ActionBarPopupThemeOverlay"
    android:id="@+id/toolbar_actionbar"
    iosched:titleTextAppearance="@style/ActionBar.TitleText"
    iosched:contentInsetStart="?actionBarInsetStart"
    android:layout_width="match_parent"
    android:layout_height="?actionBarSize" />

此工具栏布局在设置活动中引用,如下所示:

activity_settings.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".ui.SettingsActivity">

    <include layout="@layout/toolbar_actionbar_with_headerbar" />

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />
</LinearLayout>

答案 1 :(得分:2)

至于我,我通常会制作一个ToolbarActivity。接下来,如果您希望自己的活动包含工具栏,则只需YourActivity extends ToolbarActivity

public class ToolbarActivity extends AppCompatActivity {

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(R.layout.activity_toolbar);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        LayoutInflater inflater = LayoutInflater.from(this);
        View contentView = inflater.inflate(layoutResID, null);

        LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
        layout.addView(contentView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    }
}

XML:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/layout"
    tools:context=".ToolbarActivity" >

    <android.support.v7.widget.Toolbar
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:minHeight="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:id="@+id/toolbar" />

</LinearLayout>
相关问题