即使我使用NestedScrollView,ToolBar也无法彻底滚动屏幕

时间:2016-08-27 05:17:44

标签: java android xml material-design

我希望TabLayout保持在屏幕上并且滚动时工具栏不在屏幕上,但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

1 个答案:

答案 0 :(得分:1)

你能分享你的值-v21 / styles.xml吗? 从它的外观来看,您的状态栏似乎是透明的。因此,请为其设置颜色,或从android:fitsSystemWindows="true"中删除CoordinatorLayout

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

您可以查看this article,其中显示了如何使用CoordinatorLayout正确创建布局。您可以比较样式和XML。

有关fitsSystemWindows实际执行的内容的更多信息,请阅读Ian Lake的Medium article