2017-05-05 56 views
1

我想要獲得漸變,這是我的選項卡式工具欄的背景,以便在半透明狀態欄上延伸,如下圖所示。android將工具欄擴展爲半透明狀態欄,同時在systemview中保留其他佈局對象

credits to Rifayet Uday for the example

我已經嘗試使用LAYOUT_NO_LIMITS和TRANSLUCENT_NAVIGATION標誌來實現這一點,但然後在工具欄的一半的狀態欄下,去和導航欄下的小吃店去。

enter image description here

這是當前佈局文件:

<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="software.endeavor.projectg.TabbedActivity"> 

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

    <android.support.design.widget.AppBarLayout 
     android:id="@+id/appbar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:paddingTop="@dimen/appbar_padding_top" 
     android:theme="@style/AppTheme.AppBarOverlay" 
     android:background="@drawable/main_gradient"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="@drawable/main_gradient" 
      app:layout_scrollFlags="scroll|enterAlways" 
      app:popupTheme="@style/AppTheme.PopupOverlay"> 

     </android.support.v7.widget.Toolbar> 

     <android.support.design.widget.TabLayout 
      android:id="@+id/tabs" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      app:tabIndicatorColor="@android:color/white"/> 

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

    <View 
     android:id="@+id/toolbar_shadow" 
     android:layout_width="match_parent" 
     android:layout_height="12dp" 
     android:layout_below="@id/toolbar" 
     android:background="@drawable/toolbar_dropshadow" /> 

    <android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/swipeContainer" 
     android:layout_width="368dp" 
     android:layout_height="495dp"> 

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

     <android.support.design.widget.FloatingActionButton 
      android:id="@+id/fab" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="end|bottom" 
      android:layout_margin="@dimen/fab_margin" 
      app:srcCompat="@android:drawable/ic_dialog_email" /> 

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

</LinearLayout> 

我怎麼會去的標籤工具欄梯度延伸到狀態欄,同時仍保持良好的系統佈局邊界和保持常規的彩色導航欄?

回答

1

很久以前我還遇到過類似的問題。簡單的方法來解決它是使用虛擬View推動你的Toolbar和其他觀點。

虛擬視圖:

查看狀態欄的高度以推送下面的內容。

<View 
    android:layout_width="match_parent" 
    android:layout_height="@dimen/status_bar_height"/> 

現在的佈局變得:

<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="software.endeavor.projectg.TabbedActivity"> 


    <View 
     android:layout_width="match_parent" 
     android:layout_height="@dimen/status_bar_height"/> 

    ... 

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

我希望你的想法。還要注意維度中的status_bar_height值,並注意狀態欄高度在Android M或N(不確定)中發生了變化。

請參閱此文章獲取狀態欄高度: Height of status bar in Android