2016-08-27 30 views
0

我希望TabLayout保持在屏幕中,滾動時ToolBar不在屏幕上,但ToolBar始終有一部分保留在屏幕中。我在互聯網上搜索了一些代碼並運行它們,但它們具有相同的結果。即使使用了NestedScrollView,ToolBar也無法徹底滾動屏幕

<?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" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:fitsSystemWindows="true" 
tools:context="com.ihandy.a2014011385.MainActivity"> 

<android.support.design.widget.AppBarLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:theme="@style/AppTheme.AppBarOverlay"> 

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

    <android.support.design.widget.TabLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/channels_tabs"> 
    </android.support.design.widget.TabLayout> 

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

<android.support.v4.widget.NestedScrollView 
    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:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:context="com.ihandy.a2014011385.MainActivity" 
    tools:showIn="@layout/app_bar_main"> 

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

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/large_text"/> 
    </LinearLayout> 

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

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

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

和Java代碼

TabLayout channels = (TabLayout)findViewById(R.id.channels_tabs); 
channels.setTabMode(TabLayout.MODE_SCROLLABLE); 
channels.addTab(channels.newTab().setText("123")); 
channels.addTab(channels.newTab().setText("123")); 
channels.addTab(channels.newTab().setText("123")); 

這是效果現在

enter image description here

+0

使用CollapsingToolBar它將適用於您的情況 –

回答

1

你能分享你的價值觀-V21/styles.xml? 從外觀上看,您的狀態欄顯示爲透明。所以要麼爲它設置顏色,要麼從CoordinatorLayout中刪除android:fitsSystemWindows="true"

或者,您也可以將android:fitsSystemWindows="true"添加到您的AppBarLayout中。

您可以查看this article,其中顯示瞭如何使用CoordinatorLayout正確創建佈局。你可以比較你的風格和XML。

想了解更多關於fitsSystemWindows的實際情況,請閱讀Ian Lake的Medium article

+1

從'CoordinatorLayout'中刪除'android:fitsSystemWindows =「true」'適用於我。 – huangruihaocst

相關問題