工具栏与基本活动结合时无法看到高程阴影

6

我使用Android Studio创建了一个“基本活动”,并尝试在API 28的Pixel设备上创建高程阴影,使用了来自toolbar-not-showing-elevation-in-android-9-api-28 的答案。然而,没有显示高程阴影。 activity_main.xml 文件当前为:

<androidx.coordinatorlayout.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=".MainActivity">

    <com.google.android.material.appbar.AppBarLayout
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            app:elevation="0dp"
            android:theme="@style/AppTheme.AppBarOverlay">

        <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:elevation="16dp"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/AppTheme.PopupOverlay"/>

    </com.google.android.material.appbar.AppBarLayout>

    <include layout="@layout/content_main"/>

    <com.google.android.material.floatingactionbutton.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"
            app:srcCompat="@android:drawable/ic_dialog_email"/>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

为什么在Toolbar上的高程阴影不再起作用了?

你能在github上发布一个使用那个设置的简单项目吗? - azizbekian
3个回答

15

使用您提供的XML,在CoordinatorLayout中添加android:clipChildren="false",这将允许阴影绘制超出AppBarLayout的边界。

现在XML看起来像这样(这里进行了简化):

<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:clipChildren="false">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <androidx.appcompat.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            android:elevation="16dp"
            app:title="The Toolbar"
            app:titleTextColor="@android:color/white" />

    </com.google.android.material.appbar.AppBarLayout>

这将足以显示阴影。我遇到的问题是在API 28和29上,阴影非常微弱,无法与工具栏区分开。

为了使阴影更加明显,为v28创建一个样式文件,并将以下内容添加到应用程序的主题中:

<item name="android:spotShadowAlpha">0.5</item>
似乎默认的 alpha 值过于模糊,难以被轻易地看到。该值可以解决这个问题。“在 Android 中玩转高度(第一部分)”很好地解释了这个问题。现在,在 API 29 的模拟器上,布局看起来像这样:enter image description here

2
您可以设置AppBarLayoutelevation属性来实现阴影效果。
示例(在示例中,我为Toolbar设置了marginEnd和白色颜色,仅为了更容易地查看阴影效果)。
<com.google.android.material.appbar.AppBarLayout
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:elevation="16dp"
    android:layout_marginEnd="50dp"
    android:theme="@style/AppTheme.AppBarOverlay">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="#fff"
        app:popupTheme="@style/AppTheme.PopupOverlay"/>

</com.google.android.material.appbar.AppBarLayout>

我在Pixel模拟器(Android 9)和华为设备(Android 8)上进行了测试,它可以正常工作(显示阴影)。

我认为如果父ViewGroup的宽度和高度与子元素相等,则不会显示子元素的阴影。我尝试了您的情况,并使用不同的ViewGroup,如ContrainstLayoutRelativeLayout,并获得相同的行为。

最初的回答:


-1
<androidx.cardview.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="4dp"
    android:elevation="4dp">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:buttonGravity="center_vertical"
        app:titleView="@layout/toolbar_sub_category_title"
        tools:ignore="Overdraw" />
</androidx.cardview.widget.CardView>

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