2016-10-22 95 views
0

我试过搜索过几个小时,找不到任何与我想要的相关的东西。大多数问题与使导航抽屉出现在状态栏后面有关系。DrawerLayout - 在容器上的抽屉和半透明状态栏上的不透明状态栏

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

我希望状态栏在抽屉上保持不透明,但在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> 

回答

0

经过更多的搜索后,我发现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>