2017-05-05 55 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