材料设计:打开导航抽屉时更改工具栏颜色

时间:2014-11-07 09:10:38

标签: android navigation-drawer toolbar material-design

我正在尝试重新创建在Google Play商店中看到的素材设计工具栏,其中导航抽屉不会与工具栏本身重叠,但会重叠其标签。我的主要活动看起来是这样的,左边是封闭的抽屉,右边是打开的抽屉:

Screenshots

这是我的activity_main.xml:

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

    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar"/>

    <android.support.v4.widget.DrawerLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="?attr/colorPrimaryDark" />

            <FrameLayout
                android:id="@+id/spotview_content_fragment"
                android:layout_weight="2"
                android:layout_width="match_parent"
                android:layout_height="0px" />

        </LinearLayout>
        <ListView android:id="@+id/left_drawer"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:choiceMode="singleChoice"
            android:divider="@android:color/transparent"
            android:dividerHeight="0dp"
            android:background="#111"/>
    </android.support.v4.widget.DrawerLayout>
</LinearLayout>

我的toolbar.xml:

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimaryDark"/>

我还没有为抽屉添加任何其他代码。如您所见,抽屉不会与工具栏重叠,但会与标签重叠,就像我想要的那样。但是,标签会变成较暗的橙色,而工具栏则不会。如何让工具栏变得更像​​颜色,就像在Play商店中一样?我认为这样看起来会更好,因此标签和工具栏看起来就像它们在一起。提前谢谢!

1 个答案:

答案 0 :(得分:2)

如果您希望工具栏变暗,则必须将其放在DrawerLayout内并将其中的所有内容移动到工具栏下方。 DrawerLayout代码会使工具栏变暗,就像Play商店一样。

还要确保工具栏高度为@dimen/abc_action_bar_default_height_material

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


    <android.support.v4.widget.DrawerLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <include
                android:id="@+id/toolbar"
                layout="@layout/toolbar"/>


            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:layout_below="@+id/toolbar">

                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:background="?attr/colorPrimaryDark"/>

                <FrameLayout
                    android:id="@+id/spotview_content_fragment"
                    android:layout_weight="2"
                    android:layout_width="match_parent"
                    android:layout_height="0px"/>

            </LinearLayout>
        </RelativeLayout>

        <ListView
            android:id="@+id/left_drawer"
            android:layout_marginTop="@dimen/abc_action_bar_default_height_material"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:choiceMode="singleChoice"
            android:divider="@android:color/transparent"
            android:dividerHeight="0dp"
            android:background="#111"
            android:layout_below="@+id/toolbar"/>

    </android.support.v4.widget.DrawerLayout>
</LinearLayout>