10

好的,我现在已经浏览了几篇 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?

4

2 回答 2

27

工具栏是在应用程序布局中使用的操作栏的概括,现在回答您的问题有两种做法:

不良做法:

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

标准做法:

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

此标准做法将帮助您为 Toolbar 保留一个代码库,并节省您每次定义 Toolbar 的时间。

示例: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" />

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

活动设置.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>
于 2014-11-03T05:25:42.463 回答
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>
于 2015-06-17T02:04:52.407 回答