【问题标题】:DrawerLayout - Opaque Status Bar Over Drawer and Translucent Status Bar Over ContainerDrawerLayout - 抽屉上方的不透明状态栏和容器上方的半透明状态栏
【发布时间】:2016-10-22 09:15:05
【问题描述】:

我已经尝试搜索了几个小时,但找不到任何与我想要的内容相关的内容。大多数问题都与使导航抽屉出现在状态栏后面有关。

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

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

这是我所拥有的:

这就是我想要的:

我可以让状态栏在其他活动上是半透明的,除了具有 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>

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

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

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

    这是给我想要的结果的更新代码:

    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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-06
      相关资源
      最近更新 更多