工具栏在状态栏下滚动

13

我正在尝试学习CoordinatorLayout及其功能。我已经将它与AppBarLayout一起使用。我的XML代码如下:

<android.support.design.widget.CoordinatorLayout

android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:layout_scrollFlags="scroll|enterAlways" />

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

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_gravity="bottom|end"
        style="@style/FabStyle"/>

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

我面临的问题是,滚动工具栏会在状态栏下方。我希望当我滚动Recycler View时,工具栏也会随着滚动,但不要让它进入状态栏下方。如何防止这种情况发生。

此外,当我尝试将CoordinatorLayout封装在另一个布局中(例如LinearLayout)时,主题中定义的primaryDark颜色不会显示在状态栏中。当我像上面的xml一样使用它时,它会显示primaryDark颜色。有更好的方法吗?

3个回答

12

我通过进入控制状态栏颜色的样式并将其更改为我想要的颜色而修复了这个问题,而不是透明的。现在看起来状态栏在工具栏的上方。

我通过进入控制状态栏颜色的样式并将其更改为我想要的颜色而修复了这个问题,而不是透明的。现在看起来状态栏在工具栏的上方。

<resources>>
<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="android:windowDrawsSystemBarBackgrounds">true</item>
    <item name="android:statusBarColor">@android:color/transparent</item>
</style>
<resources>>
<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="android:windowDrawsSystemBarBackgrounds">true</item>
    <item name="android:statusBarColor">@color/colorPrimaryDark</item>
</style>

:这是一个空的段落标记。

8

我曾经遇到过类似的问题,我的问题已经解决了。

在AppBarLayout中添加android:fitsSystemWindows="true"。 为Toolbar添加app:layout_scrollFlags="scroll|enterAlways|snap" 下面是我使用的布局。

<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"
tools:context=".HomeActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fitsSystemWindows="true"
    android:theme="@style/AppTheme.AppBarOverlay">
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways|snap"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

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

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

<android.support.v4.view.ViewPager
    android:id="@+id/id_viewpager"
    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="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    android:src="@android:drawable/ic_dialog_email"/>

我错误地将 android:fitsSystemWindows="true" 添加到了 CoordinatorLayout 中。 现在的问题已经通过从 CoordinatorLayout 中删除 android:fitsSystemWindows="true" 并将其添加到 AppbarLayout 中来解决。 - superus8r

0
你可以在根视图中添加 CoordinatorLayoutfitsystemwindows = true

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接