android - 如果半透明导航为真,AppBarLayout 中的工具栏不会滚动

标签 android scroll navigation-drawer android-toolbar android-coordinatorlayout

更新:这是一个 example app .我已经在我的 Nexus 6P、Android 6.0.1 上测试过了

我正在使用 CoordinatorLayoutRecyclerView . 如果<item name="android:windowTranslucentNavigation">true</item>工具栏没有隐藏(但它本身是可滚动的)。 appcompat 是 v7:23.2.1

<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">

    <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:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

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

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

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

如果我添加 android:fitsSystemWindows="true"AppBarLayout它可以解决一些问题。

有什么解决办法吗?

系统栏覆盖工具栏 enter image description here

有顶部和底部填充 enter image description here

CoordinatorLayout (也许)滚动工具栏 enter image description here

最佳答案

看来这是一个错误。我找到了该问题的临时解决方案。

删除 Coordinator Layout 及其子项中的所有 android:fitsSystemWindows,或将它们设置为 false。然后在您的 Activity 中手动将状态栏的高度添加到工具栏的高度和顶部填充:

toolbar.setPadding(0, getStatusBarHeight(), 0, 0);
toolbar.getLayoutParams().height = toolbar.getLayoutParams().height + getStatusBarHeight();

获取状态栏高度的方法:

public int getStatusBarHeight() {
    int height = 0;
    int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
    if (resourceId > 0) {
        height = getResources().getDimensionPixelSize(resourceId);
    }
    return height;
}

通过这些更改,工具栏可以正确滚动,并且具有适当的高度。

关于android - 如果半透明导航为真,AppBarLayout 中的工具栏不会滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36224255/

相关文章:

Android- ScrollView 和水平回收 View 之间的滑动冲突

android - 当将 EditText 聚焦在 ListView 或 RecyclerView 中时,键盘显示但滚动不起作用

android - 抽屉导航中的 Google map - 空指针异常,哪个是正确的 fragment ?

android - 导航 View 菜单项不响应任何单击

android - 抽屉导航不保留所选项目

android - 如何安排在计时器完成后运行的 sqlite 查询?

android - 如何将时钟或日历附加到我的应用程序?

java - 项目单击监听器不适用于自动滚动回收器 View

java - 如果高分大于 20,则无法选中复选框

java - 在 Android 中禁用短信和电子邮件通知