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

时间:2014-10-30 21:30:31

标签: android material-design

我正在将Material Design应用到我的带有导航抽屉的应用程序中。在Nav的所有不同实现中。具有材料设计的抽屉和工具栏(see this post);我选择让应用程序感觉类似于ICS / Halo设计,并将Nav Drawer滑出工具栏。问题是当导航抽屉打开时,工具栏会像活动的其余部分一样变暗。如何防止工具栏变暗?如果您在我上面链接的帖子中看到图像,我在#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 <FrameLayout>的{​​{1}}是我的所有片段都被充气的地方,其'container'被设置为工具栏的高度,因此其内容将是在工具栏下方。同样,导航抽屉片段也将其marginTop设置为工具栏的高度,以便在其下方滑出。

2 个答案:

答案 0 :(得分:9)

感谢@alanv我修复了我的问题。

我上面的帖子显示了我的activity_main.xml代码;我将其简化为此,删除工具栏视图并删除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,我现在在我的主要活动中设置了SessionContent()<include>是上面的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>

最后,在onCreate()的{​​{1}} setContentView() toolbar.xml我的活动中{/ 1}}

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

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

答案 1 :(得分: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>