【问题标题】:Toolbar xml shadow (elevation) can get working only on Android 21+工具栏 xml 阴影(高度)只能在 Android 21+ 上运行
【发布时间】:2019-08-15 02:18:50
【问题描述】:

所以我添加了导航抽屉,因此我必须在我的布局 xml 文件中使用工具栏(而不是使用带有操作栏的主题 Theme.AppCompat.Light.DarkActionBar,现在是 Theme.AppCompat.Light.NoActionBar

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

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"/>

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

我找到了在工具栏下方添加阴影的答案 (AppBarLayout) https://stackoverflow.com/a/31026359/9766649

但它仅适用于 Android 21+

Theme.AppCompat.Light.DarkActionBar shadow 适用于旧版 Android

所以唯一的解决方案是使用像https://stackoverflow.com/a/26904102/9766649 这样的自定义阴影?

【问题讨论】:

    标签: android android-toolbar android-elevation


    【解决方案1】:

    我决定为新旧 Android 使用不同的实现

    对于 Android 21+ (layout-v21/activity_main.xml):

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <com.google.android.material.appbar.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            xmlns:android="http://schemas.android.com/apk/res/android">
    
            <include layout="@layout/toolbar_view"/>
    
        </com.google.android.material.appbar.AppBarLayout>
    
        <include layout="@layout/content"/>
    
    </LinearLayout>
    

    对于旧版 Android (layout/activity_main.xml):

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <include layout="@layout/toolbar_view"/>
    
        <FrameLayout android:layout_width="match_parent"
                     android:layout_height="match_parent">
    
            <include layout="@layout/content"/>
    
            <View android:layout_width="match_parent"
                  android:layout_height="5dp"
                  android:background="@drawable/drop_shadow"/>
    
        </FrameLayout>
    
    </LinearLayout>
    

    toolbar_view.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:theme="@style/ThemeOverlay.AppTheme.ActionBar"
        xmlns:android="http://schemas.android.com/apk/res/android"/>
    

    drawable/drop_shadow.xml(在旧版 Android 的工具栏下方添加高度):

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
           android:shape="rectangle">
    
        <gradient android:startColor="@android:color/transparent"
                  android:endColor="#88666666"
                  android:angle="90"/>
    
    </shape>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-03
      • 2016-04-30
      • 2023-03-27
      • 2023-04-01
      • 2020-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多