我使用 Android Studio 创建了一个“基本 Activity ”,并尝试使用来自 toolbar-not-showing-elevation-in-android-9-api-28 的答案在 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
上不再起作用?
最佳答案
使用您提供的 XML,将 android:clipChildren="false"
添加到 CoordinatorLayout。这将允许在 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 值似乎太微弱了,不容易被看到。此值可解决问题。
"Playing with elevation in Android (part 1)"很好地解释了这个问题。
布局现在在 API 29 模拟器上看起来像这样:
关于android - 对于具有基本 Activity 的工具栏,高程阴影不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57066909/