【问题标题】:Material design: Change toolbar color when navigation drawer is opened材料设计:打开导航抽屉时更改工具栏颜色
【发布时间】:2014-11-07 09:10:38
【问题描述】:

我正在尝试重新创建在 google play 商店中看到的材料设计工具栏,其中导航抽屉不会与工具栏本身重叠,但会与它的选项卡重叠。我的主要活动如下所示,左侧是关闭的抽屉,右侧是打开的抽屉:

这是我的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 商店中一样变得更暗?我认为这样看起来会更好,因此选项卡和工具栏看起来像是属于一起的。提前致谢!

【问题讨论】:

    标签: android navigation-drawer toolbar material-design


    【解决方案1】:

    如果您希望工具栏变暗,则必须将其放在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>
    

    【讨论】:

    • 如果我在 DrawerLayout 中移动工具栏,抽屉也会与工具栏重叠,这是我开始使用的,也是我不想要的。如果我将工具栏放在那里,如何确保抽屉不重叠?
    • 我正在为你构建一个 xml。很快就会发布
    • 现在请检查答案。
    • @AndroidCoder85 还要确保你的工具栏有固定的高度(@dimen/abc_action_bar_default_height_material
    • 所以如果我理解正确 listview 中的 margin_top 让抽屉从 x=abc_action_bar_default_height_material 开始?这似乎应该工作。我将在今天晚些时候对其进行测试,如果可行,我将接受您的回答。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-30
    • 1970-01-01
    • 2014-12-21
    • 1970-01-01
    • 1970-01-01
    • 2016-03-25
    相关资源
    最近更新 更多