新的 Android 设计库中 AppBarLayout 和 Toolbar 存在 bug。

31

我正在使用基于chrisbanes/cheesesquare在GitHub上的示例和这里的新Android Design Library。

我已经运行了示例,但是在CheeseDetailActivity内部的Toolbar出现了问题。工具栏没有像应该显示的那样。请查看下面的图片:

第一张图片中,您可以看到工具栏未正确显示。

enter image description here

在第二张图片中,您可以看到工具栏被正确显示,但通知栏是白色的。这是因为我从activity_detail.xml中删除了android.support.design.widget.CoordinatorLayout中的android:fitsSystemWindows="true"

enter image description here

我认为fitsSystemWindows应该为true,问题与android.support.design.widget.AppBarLayout有关,但我不知道如何解决这个问题。我尝试使用与notificationBar相同高度的marginTop,但它没有起作用。

非常感谢任何建议 :)

这是activity_detail.xml的一部分:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="@dimen/detail_backdrop_height"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    android:fitsSystemWindows="true">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:expandedTitleMarginStart="48dp"
        app:expandedTitleMarginEnd="64dp">

        <ImageView
            android:id="@+id/backdrop"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="centerCrop"
            android:fitsSystemWindows="true"
            app:layout_collapseMode="parallax" />

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:layout_collapseMode="pin" />

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

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

你在哪里设置了 app:layout_behavior - Rod_Algonquin
1
我没有展示所有的XML,但你可以在这里找到它:https://github.com/chrisbanes/cheesesquare/blob/master/app/src/main/res/layout/activity_detail.xml - Ultimo_m
我不想改变状态栏的颜色,我希望它看起来像这样:https://youtu.be/32i7ot0y78U?t=4m13s - Ultimo_m
我明白了。在AppBarLayout上使用android:fitsSystemWindows="true"是因为你想让它出现在状态栏后面,而在Toolbar上使用android:fitsSystemWindows="false"是因为你不想让它出现在状态栏后面。 - Eugen Pechanec
@EugenPechanec 这个不行,我已经试过了,而且我还注意到如果你把完全相同的布局放在viewpager里面,nestedScrollView总是会有一些paddingTop,除非你触摸它,然后它才会消失。我尝试过requestLayout、buildLayer、invalidate等方法都没有用。 - Kosh
5个回答

18

app文件夹中的build.gradle文件中使用以下代码更改您的Design Library版本:

compile 'com.android.support:design:22.2.1'

如在+AndroidDevelopers中更新。

我得到了以下输出:

enter image description here

这将有所帮助。

谢谢 :)


我还没有测试,但那应该是解决方案 :) - Ultimo_m
Android支持设计库版本22.2.1对我来说并没有解决问题。在运行Android 5.0的设备上,我又遇到了麻烦。我不得不升级到23.1.0版本。现在它可以正常工作了。 - Sandra

8

1
希望他们能快速修复它 :) - Ultimo_m
@Ultimo_m 有任何更新吗? - Pratik Butani
@PratikButani,根据这篇文章的更新https://plus.google.com/+AndroidDevelopers/posts/XTtNCPviwpj,他们已经进行了更新。我还没有检查它是否解决了这个问题。 - Ultimo_m

1
我有相同的问题,通过使用windowActionBar和windowNoTitle样式来解决。
<style name="AppTheme.base" parent="Base.Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
       <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

0

我也有同样的问题,我的工具栏在API级别大于21时显示错误。 我正在使用android.support.v7.widget.Toolbar作为supportActionBar(),并且以下内容位于片段中,请参见图片: 应用程序启动时,工具栏显示不正确当我折叠android.support.design.widget.CollapsingToolbarLayout时,图片没有完全隐藏

当我在Toolbar所在的视图的根元素中添加了android:fitsSystemWindows="true"属性时,我解决了这个问题。
现在: 工具栏正常显示图片完全隐藏

0

这里有一些适用于API 21的有效解决方法:

 if (Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP) {
    marginResult = 0;
    int resourceId = getResources().getIdentifier(getString(R.string.identifier_status_bar_height), getString(R.string.identifier_dimen), getString(R.string.identifier_android));    
    if (resourceId > 0) {
        marginResult = getResources().getDimensionPixelSize(resourceId)*2;
     }
    CollapsingToolbarLayout.LayoutParams params = (CollapsingToolbarLayout.LayoutParams) mToolbar.getLayoutParams();
    params.topMargin -= marginResult;
    mToolbar.setLayoutParams(params);}

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