android设计库工具栏阴影

标签 android toolbar shadow

<分区>

尝试新的 android 设计库时,我发现了一个工具栏阴影错误。使用 CollapsingToolbarLayout 时,工具栏下方的阴影仅在工具栏折叠时出现。当我们展开它时,阴影消失了。我的布局看起来像

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
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="wrap_content"
    android:fitsSystemWindows="true"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

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

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="@dimen/user_avatar_height"
            android:fitsSystemWindows="true"
            android:scaleType="centerCrop"
            app:layout_collapseMode="parallax" />

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

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

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="@color/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        app:tabGravity="fill"
        app:tabIndicatorColor="@android:color/white"
        app:tabMaxWidth="0dp"
        app:tabMode="fixed" />

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

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

和结果 enter image description here 工具栏折叠时有阴影。

enter image description here

但展开后阴影消失。

有什么办法可以解决这个问题吗? 可能有办法处理阴影的出现/消失吗? 谢谢。

最佳答案

更新

Google 的支持库团队似乎将此行为作为 24.0.0 的一部分进行了介绍,但它存在一些问题,因此现在您需要采取新的解决方法。

1 - 在 res 目录下的 animator-v21 文件夹中创建 appbar_always_elevated.xml。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item>
        <objectAnimator android:propertyName="elevation"
                        android:valueTo="8dp"
                        android:valueType="floatType"
                        android:duration="1"/>
    </item>

</selector>

2 - 在 AppBarLayout 中使用:

<android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:fitsSystemWindows="true"
        android:stateListAnimator="@animator/appbar_always_elevated"
        android:theme="@style/AppTheme.AppBarOverlay">

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

旧答案

他们将此作为新支持库版本 (24) 的一部分进行了修复,因此您只需要升级并使用正确的属性即可。

watch https://youtu.be/w45y_w4skKs?t=31m52s

enter image description here


旧答案

我通过围绕 ViewPager 设置 FrameLayout 并设置 android:foreground 解决了这个问题。

附件是我正在使用的可绘制对象,这里是 gist 的链接

enter image description here

关于android设计库工具栏阴影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32474073/

相关文章:

java - 特定用户的所有通知都将从数据库中删除,而实际上只应删除一条通知

delphi - TToolbar 与 TForm.DoubleBuffered 不兼容?

flutter - 如何在 Flutter 中向 TextFormField 添加阴影

ios - 动画 CALayer 的 shadowPath 属性

html - 复杂的 CSS 文本阴影和跨浏览器兼容性

android - Listview 和 volley - 图像分配错误

android - GAE 无法识别 Cookie?

java - 无法选中复选框

c++ - 尝试使用 ImageList 创建工具栏,但不起作用

c# - IE8/IE9 工具栏 Cookie 检查