材料工具栏上的高度设置不会产生阴影。

3
正如标题所述,我尝试了多种android:elevation和app:elevation的组合,但我不知道这是什么,可能我错过了一些显而易见的东西。
xml:
<?xml version="1.0" encoding="utf-8"?>
<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"
    android:orientation="vertical"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.example.jabotmobile.MainActivity"
    android:background="@color/white">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/dark"
        app:elevation="8dp">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:background="?attr/colorPrimary"/>

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

    <FrameLayout
        android:id="@+id/fragmentContainer1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/red_light"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:elevation="0dp"/>


</androidx.coordinatorlayout.widget.CoordinatorLayout>
1个回答

3

您需要将android:clipChildren="false"添加到父视图中。

<androidx.coordinatorlayout.widget.CoordinatorLayout 
    android:clipChildren="false">

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

        <androidx.appcompat.widget.Toolbar
           app:elevation="8dp"
        />

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

从api28版本开始,默认情况下,阴影将应用alpha通道。

您可以通过在应用主题中添加android:spotShadowAlpha属性来更改此值(仅适用于api28+):

<!--Alpha value of the spot shadow projected by elevated views, between 0 and 1.-->
<item name="android:spotShadowAlpha">0.x</item>

enter image description here


1
哦,我不知道这个。这帮助我摆脱了使用边距的丑陋感!谢谢你! - private static

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