DrawerLayout - 抽屉上的不透明状态栏和容器上的半透明状态栏

时间:2016-10-22 09:15:06

标签: android android-layout

我试过几个小时的搜索,却找不到任何与我想要的东西有关的东西。大多数问题都与使导航抽屉显示在状态栏后面有关系。

但是,这不是我想要的行为。

我希望状态栏在Drawer上保持不透明,但在DrawerLayout容器(片段)上是透明的。

以下是我所拥有的:

enter image description here

这就是我想要的:

enter image description here

除了具有DrawerLayout的主要活动之外,我可以让状态栏对其他活动半透明。因为每当我尝试使状态栏半透明时,只有抽屉可以变成半透明,这与我想要的抽屉完全相反。状态栏应该只对片段半透明。

我是如何实现这一目标的?

activity_main.xml中:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:openDrawer="start"
    android:fitsSystemWindows="true">

    <!-- This FrameLayout is the container for all other fragments
    so I want this to have a transparent status bar because some fragments
    may have "cover photos" that needs to take up the status bar. It looks
    nicer that way. I tried playing around with the fragment layouts    
    themselves. Bit nothing seems to budge, so I give up on that part. -->
    <FrameLayout 
        android:id="@+id/main_fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </FrameLayout>

    <android.support.design.widget.NavigationView
        android:fitsSystemWindows="false"
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/activity_main_nav_header_main"
        app:menu="@menu/activity_main_drawer"/>

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

fragment_profile.xml (要放入容器内的一个片段)

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#fff"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways">


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

                <android.support.v7.widget.CardView
                    android:id="@+id/card_cover"
                    android:layout_width="match_parent"
                    android:layout_height="200dp"
                    app:cardElevation="10dp">

                    <ImageView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:background="#11b4ff"
                        android:src="@android:drawable/sym_def_app_icon"/>
                </android.support.v7.widget.CardView>

                <android.support.v7.widget.CardView
                    android:id="@+id/card_profile"
                    android:layout_below="@id/card_cover"
                    android:layout_width="120dp"
                    android:layout_height="120dp"
                    app:cardElevation="10dp"
                    android:layout_marginLeft="15dp"
                    android:layout_marginBottom="15dp"
                    android:layout_marginTop="-60dp">
                    <ImageView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:background="#1119ff"
                        android:src="@android:drawable/sym_def_app_icon"
                        />
                </android.support.v7.widget.CardView>

                <TextView
                    android:layout_margin="15dp"
                    android:layout_below="@id/card_cover"
                    android:layout_toRightOf="@id/card_profile"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Profile Name"
                    android:textSize="30sp"/>

            </RelativeLayout>

            <android.support.v7.widget.Toolbar
                android:id="@+id/fragment_toolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>

        </android.support.design.widget.CollapsingToolbarLayout>

        <android.support.design.widget.TabLayout
            android:id="@+id/tabLayout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            app:tabIndicatorColor="@android:color/white"
            app:tabIndicatorHeight="6dp"
            app:tabMode="scrollable"
            app:tabSelectedTextColor="@android:color/white"
            app:tabTextColor="@android:color/white"/>

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>


</android.support.design.widget.CoordinatorLayout>

V21 \ styles.xml

<resources>
    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:windowTranslucentStatus">true</item>
        <item name="android:windowTranslucentNavigation">true</item>
        <item name="android:statusBarColor">@android:color/transparent</item>
    </style>
</resources>

1 个答案:

答案 0 :(得分:0)

经过多次搜索,我发现this回答虽然没有直接解决我的问题,但它给了我关于发生了什么的线索。为了在片段中启用半透明状态栏,我需要将容器布局更改为CoordinatorLayout。

此外,我需要添加&#34; fitsSystemWindows = true&#34;所有子视图,以便将插图正确放置在我想要的片段中。

这里是给我提供所需结果的更新代码:

<强> activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:openDrawer="start"
    android:fitsSystemWindows="true">

    <android.support.design.widget.CoordinatorLayout
        android:fitsSystemWindows="true"
        android:id="@+id/main_fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </android.support.design.widget.CoordinatorLayout>

    <android.support.design.widget.NavigationView
        android:fitsSystemWindows="false"
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/activity_main_nav_header_main"
        app:menu="@menu/activity_main_drawer"/>

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

<强> fragment_profile.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:fitsSystemWindows="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.design.widget.CollapsingToolbarLayout
            android:fitsSystemWindows="true"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#fff"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways">

            <RelativeLayout
                android:fitsSystemWindows="true"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <android.support.v7.widget.CardView
                    android:id="@+id/card_cover"
                    android:layout_width="match_parent"
                    android:layout_height="200dp"
                    app:cardElevation="10dp">

                    <ImageView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:background="#11b4ff"
                        android:src="@android:drawable/sym_def_app_icon"/>
                </android.support.v7.widget.CardView>

                <android.support.v7.widget.CardView
                    android:id="@+id/card_profile"
                    android:layout_below="@id/card_cover"
                    android:layout_width="120dp"
                    android:layout_height="120dp"
                    app:cardElevation="10dp"
                    android:layout_marginLeft="15dp"
                    android:layout_marginBottom="15dp"
                    android:layout_marginTop="-60dp">
                    <ImageView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:background="#1119ff"
                        android:src="@android:drawable/sym_def_app_icon"
                        />
                </android.support.v7.widget.CardView>

                <TextView
                    android:layout_margin="15dp"
                    android:layout_below="@id/card_cover"
                    android:layout_toRightOf="@id/card_profile"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Profile Name"
                    android:textSize="30sp"/>

            </RelativeLayout>

            <android.support.v7.widget.Toolbar
                android:id="@+id/fragment_toolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>

        </android.support.design.widget.CollapsingToolbarLayout>

        <android.support.design.widget.TabLayout
            android:id="@+id/tabLayout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            app:tabIndicatorColor="@android:color/white"
            app:tabIndicatorHeight="6dp"
            app:tabMode="scrollable"
            app:tabSelectedTextColor="@android:color/white"
            app:tabTextColor="@android:color/white"/>

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

</android.support.design.widget.CoordinatorLayout>