打开导航抽屉时从工具栏中移除阴影 - Material Design 支持库。

7
我正在将Material Design应用于我的应用程序,该应用程序具有导航抽屉。在所有不同的Nav. Drawer和Toolbar与Material Design的实现中(请参见此帖子);我选择保持应用程序感觉类似于ICS / Halo设计,并使Nav抽屉从工具栏下滑出。问题是当导航抽屉打开时,工具栏会像活动的其余部分一样变暗。我该如何保持工具栏不变暗?如果您查看我上面链接的帖子中的图像,我想要#6、3或5,但现在我的外观更像#9。
示例(来自上面的帖子):
我想要的(工具栏上没有阴影):

enter image description here

我目前得到的(当导航抽屉打开时,工具栏会变暗):

enter image description here

这是我的主活动XML的代码:

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.funkhaus.navdrawer.app.MainActivity">

<!-- As the main content view, the view below consumes the entire
     space available using match_parent in both dimensions. -->
<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- We use a Toolbar so that our drawer can be displayed
         in front of the action bar; Added for Material Design -->
    <android.support.v7.widget.Toolbar
        android:id="@+id/my_awesome_toolbar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:minHeight="?attr/actionBarSize"
        android:background="?attr/colorPrimary" />

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="?attr/actionBarSize" />

</FrameLayout>

<fragment android:id="@+id/navigation_drawer"
    android:layout_width="@dimen/navigation_drawer_width"
    android:layout_height="match_parent"
    android:layout_marginTop="?attr/actionBarSize"
    android:fitsSystemWindows="true"
    android:layout_gravity="start"
    android:name="com.funkhaus.brewwerks.NavigationDrawerFragment" />

值得注意的是,ID为“container”的<FrameLayout>是我所有片段被填充的地方,它的marginTop被设置为工具栏的高度,以便其内容位于工具栏下方。同样,导航抽屉片段也将其marginTop设置为工具栏的高度,以便在其下方滑出。

1
将工具栏放在 DrawerLayout 之外。 - alanv
据我所知,当使用导航抽屉(Navigation Drawer)时,<DrawerLayout> 需要成为根视图。 - Flash
1
你的内容根目录,是的。不是你的工具栏根目录。 - alanv
谢谢@alanv,如果我发布的答案看起来合理,请告诉我。 - Flash
2个回答

9

感谢 @alanv 帮我解决了问题。

上面的帖子展示了我的 activity_main.xml 代码;我简化了它,删除了 Toolbar 视图并移除了 marginTop 格式设置。

<android.support.v4.widget.DrawerLayout
    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:fitsSystemWindows="true"
    tools:context="com.funkhaus.navdrawer.app.MainActivity">

<!-- As the main content view, the view below consumes the entire
     space available using match_parent in both dimensions. -->
    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <fragment android:id="@+id/navigation_drawer"
        android:layout_width="@dimen/navigation_drawer_width"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        android:layout_gravity="start"
        android:name="com.funkhaus.brewwerks.NavigationDrawerFragment" />

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

我创建了一个toolbar.xml文件,现在我在主Activity中使用setContentView()方法将其包含在activity_main.xml之上:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical">
    <!-- We use a Toolbar so that our drawer can be displayed
             in front of the action bar; Added for Material Design -->
    <android.support.v7.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/my_awesome_toolbar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:minHeight="?attr/actionBarSize"
        android:background="?attr/colorPrimary" />

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

</LinearLayout>

最后,在我的Activity的onCreate()方法中,我通过setContentView()方法设置了toolbar.xml布局文件:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.toolbar);
    // Rest of code here....

1
ActionBar仍然受支持,为什么不使用它呢?它的行为与此完全相同。 - CristianGuerrero
1
非常感谢!@The Funk - theknut
尽管我们都知道正在根据更新的Material Design指南设计导航抽屉,但这里有一个链接到更新的指南:http://www.google.com/design/spec/patterns/navigation-drawer.html - Flash

2
You do it in the same xml like this, This is for underlying toolbar navigation like play store app design




  <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.ccas.roadsideconnect.BaseFragmentActivity">
    <android.support.v4.widget.DrawerLayout

        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >


        <FrameLayout
            android:id="@+id/main_fragment_container"
            android:layout_marginTop="?attr/actionBarSize"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />



        <FrameLayout android:id="@+id/navigation_drawer"
            android:layout_width="@dimen/navigation_drawer_width"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            tools:layout="@layout/fragment_navigation_drawer" />
    </android.support.v4.widget.DrawerLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="@drawable/img_actionbar_white_sm"
                android:gravity="top"
                android:minHeight="58.0dip"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:theme="@style/ThemeOverlay.AppCompat.ActionBar"
                />
        </LinearLayout>

    </FrameLayout>

以下代码用于实现类似 Gmail APP 设计的覆盖式工具栏导航。
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.ccas.roadsideconnect.BaseFragmentActivity">
<android.support.v4.widget.DrawerLayout

    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <FrameLayout
        android:id="@+id/main_fragment_container"
        android:layout_marginTop="?attr/actionBarSize"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@drawable/img_actionbar_white_sm"
            android:gravity="top"
            android:minHeight="58.0dip"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:theme="@style/ThemeOverlay.AppCompat.ActionBar"
            />
    </LinearLayout>

    <FrameLayout android:id="@+id/navigation_drawer"
        android:layout_width="@dimen/navigation_drawer_width"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        tools:layout="@layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>

</FrameLayout>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接