0

我遇到了一個問題,當我使用CoordinatorLayout,其中包含imageview 的片段時,我無法使狀態欄變透明並在狀態欄下繪製imageview(in活動沒關係)。如何在片段中使用CoordinatorLayout時使狀態欄透明

爲了說明問題,我以cheesesquare爲例。

有一個在cheesesquare一個CheeseDetailActivity顯示一些這樣的內容(這是我想要的效果):

in activity 正如我們所知,它容易使活動這種效果,我拿起鑰匙代碼:

  • styles.xml(V21)

    <資源>

    <樣式名稱= 「Theme.DesignDemo」 父= 「Base.Theme.DesignDemo」> <項目名稱= 「機器人:windowDrawsSystemBarBackgrounds」>真 <項目名稱= 「機器人:statusBarColor」> @安卓:彩色/透明 < /風格>

< /資源>

  • activity_detail.xml

    <android.support.design.widget.CollapsingToolbarLayout 
        android:id="@+id/collapsing_toolbar" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        app:layout_scrollFlags="scroll|exitUntilCollapsed" 
        android:fitsSystemWindows="true" 
        app:contentScrim="?attr/colorPrimary" 
        app:expandedTitleMarginStart="48dp" 
        app:expandedTitleMarginEnd="64dp"> 
    
        <ImageView 
         android:id="@+id/backdrop" 
         android:layout_width="match_parent" 
         android:layout_height="match_parent" 
         android:scaleType="centerCrop" 
         android:fitsSystemWindows="true" 
         app:layout_collapseMode="parallax" /> 
    
        <android.support.v7.widget.Toolbar 
         android:id="@+id/toolbar" 
         android:layout_width="match_parent" 
         android:layout_height="?attr/actionBarSize" 
         app:popupTheme="@style/ThemeOverlay.AppCompat.Light" 
         app:layout_collapseMode="pin" /> 
    
    </android.support.design.widget.CollapsingToolbarLayout> 
    

    <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:orientation="vertical" 
        android:paddingTop="24dp"> 
    
        <android.support.v7.widget.CardView 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:layout_margin="@dimen/card_margin"> 
    
         <LinearLayout 
          style="@style/Widget.CardContent" 
          android:layout_width="match_parent" 
          android:layout_height="wrap_content"> 
    
          <TextView 
           android:layout_width="match_parent" 
           android:layout_height="wrap_content" 
           android:text="Info" 
           android:textAppearance="@style/TextAppearance.AppCompat.Title" /> 
    
          <TextView 
           android:layout_width="match_parent" 
           android:layout_height="wrap_content" 
           android:text="@string/cheese_ipsum" /> 
    
         </LinearLayout> 
    
        </android.support.v7.widget.CardView> 
    
        <android.support.v7.widget.CardView 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:layout_marginBottom="@dimen/card_margin" 
         android:layout_marginLeft="@dimen/card_margin" 
         android:layout_marginRight="@dimen/card_margin"> 
    
         <LinearLayout 
          style="@style/Widget.CardContent" 
          android:layout_width="match_parent" 
          android:layout_height="wrap_content"> 
    
          <TextView 
           android:layout_width="match_parent" 
           android:layout_height="wrap_content" 
           android:text="Friends" 
           android:textAppearance="@style/TextAppearance.AppCompat.Title" /> 
    
          <TextView 
           android:layout_width="match_parent" 
           android:layout_height="wrap_content" 
           android:text="@string/cheese_ipsum" /> 
    
         </LinearLayout> 
    
        </android.support.v7.widget.CardView> 
    
        <android.support.v7.widget.CardView 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:layout_marginBottom="@dimen/card_margin" 
         android:layout_marginLeft="@dimen/card_margin" 
         android:layout_marginRight="@dimen/card_margin"> 
    
         <LinearLayout 
          style="@style/Widget.CardContent" 
          android:layout_width="match_parent" 
          android:layout_height="wrap_content"> 
    
          <TextView 
           android:layout_width="match_parent" 
           android:layout_height="wrap_content" 
           android:text="Related" 
           android:textAppearance="@style/TextAppearance.AppCompat.Title" /> 
    
          <TextView 
           android:layout_width="match_parent" 
           android:layout_height="wrap_content" 
           android:text="@string/cheese_ipsum" /> 
    
         </LinearLayout> 
    
        </android.support.v7.widget.CardView> 
    
    </LinearLayout> 
    

  • 現在的問題

,出於某種原因,我想表明的片段,但不是在activity.I嘗試修改cheesesquare。我的代碼相同的內容只需添加一個片段和style.xml和layout.xml幾乎same.Then問題存在:

enter image description here

狀態欄瞧瞧不同!我確認樣式和佈局是相同的。所以我的問題是什麼使不同?我怎樣才能使狀態欄透明?

編輯:

  • 的個XML修改

activity_main.xml中後(經修飾)

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

<!--<include layout="@layout/include_list_viewpager"/>--> 

<FrameLayout 
    android:id="@+id/fragment_container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <fragment 
     class="com.support.android.designlibdemo.MainFragment" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"/> 

</FrameLayout> 

<android.support.design.widget.NavigationView 
    android:id="@+id/nav_view" 
    android:layout_height="match_parent" 
    android:layout_width="wrap_content" 
    android:layout_gravity="start" 
    android:fitsSystemWindows="true" 
    app:menu="@menu/drawer_view"/> 

fragment_main.xml(添加)

<FrameLayout 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" 
     tools:context="com.support.android.designlibdemo.MainFragment"> 

<include layout="@layout/include_list_viewpager"/> 

include_list_viewpager.xml(沒有修改)

style.xml(沒有修改)

activity_cheese_detail.xml(刪除)

fragment_cheese_detail.xml(添加,只複製源activity_cheese_detail.xml)

<android.support.design.widget.CoordinatorLayout 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/main_content" 
              android:layout_width="match_parent" 
              android:layout_height="match_parent" 
              android:fitsSystemWindows="true" 
              tools:context=".MainActivity"> 

<android.support.design.widget.AppBarLayout 
    android:id="@+id/appbar" 
    android:layout_width="match_parent" 
    android:layout_height="@dimen/detail_backdrop_height" 
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
    android:fitsSystemWindows="true"> 

    <android.support.design.widget.CollapsingToolbarLayout 
     android:id="@+id/collapsing_toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_scrollFlags="scroll|exitUntilCollapsed" 
     android:fitsSystemWindows="true" 
     app:contentScrim="?attr/colorPrimary" 
     app:expandedTitleMarginStart="48dp" 
     app:expandedTitleMarginEnd="64dp"> 

     <ImageView 
      android:id="@+id/backdrop" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:scaleType="centerCrop" 
      android:fitsSystemWindows="true" 
      app:layout_collapseMode="parallax" /> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      app:popupTheme="@style/ThemeOverlay.AppCompat.Light" 
      app:layout_collapseMode="pin" /> 

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

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

<android.support.v4.widget.NestedScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

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

<android.support.design.widget.FloatingActionButton 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    app:layout_anchor="@id/appbar" 
    app:layout_anchorGravity="bottom|right|end" 
    android:src="@drawable/ic_discuss" 
    android:layout_margin="@dimen/fab_margin" 
    android:clickable="true"/> 

+0

添加片段後請顯示xml。 – GPack

+0

好的,問題被編輯。 –

回答

0

你必須使用奇巧+安卓有這個選項。

將此代碼添加到您的主題:

<item name="android:windowTranslucentStatus">true</item> 
<item name="android:windowTranslucentNavigation">true</item> 
2

你可以這的Java代碼

protected void makeTransperantStatusBar(boolean isTransperant) { 
    if (isTransperant) { 
     getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); 
    } else { 
     getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); 
    } 
} 
如果你想你應該做以下

中添加這些行完全透明單位

你的主題

<item name="android:windowTranslucentStatus">true</item> 
<item name="android:windowTranslucentNavigation">true</item> 

設置這個標誌在統籌佈局

android:fitsSystemWindows="true" 

在最後在Activity

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { 
     Window w = getWindow(); // in Activity's onCreate() for instance 
     w.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS); 
    } 

參考solution taken from here

1

刪除或更改爲CoordinatorLayout類型(onCreate方法添加這些行with fitSystemWindows)CoordinatorLayout子項的FrameLayout父項。

+0

我很抱歉...你能詳細說明一下嗎?我不是很清楚 –

+0

FrameLayout不會將insets信息發送給它的孩子。使用CoordinatorLayout類型作爲嵌套CoordinatorLayout的父類型。 – GPack

相關問題