为什么工具栏阴影无法在Android中删除?

问题描述

我正在尝试删除工具栏底部的阴影。我已将 android:elevation =“ 0dp” 应用于AppBar和工具栏仍然无法正常工作。

在本文中,我为Remove shadow between Toolbar and TabLayout应用了各种解决方案,以消除阴影仍然没有效果

我的MainActivity.xml代码:具有工具栏

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout

    android:fitsSystemWindows="true"
    android:background="?attr/activityBackground"
    android:id="@+id/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        
    <com.google.android.material.appbar.AppBarLayout
        android:elevation="0dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <androidx.appcompat.widget.Toolbar
            android:elevation="0dp"
            app:layout_scrollFlags="scroll|enteralways"
            app:titleTextColor="?toolbarTitleColor"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/toolbarColor"
            android:id="@+id/toolbar1"
            app:title="Home">
            .
            .
            .
            />

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

        <FrameLayout
            android:layout_marginBottom="56dp"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            android:id="@+id/birthdayFragmentHolder"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>


        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:layout_gravity="bottom"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </androidx.coordinatorlayout.widget.CoordinatorLayout>

    </com.google.android.material.navigation.NavigationView>

</androidx.drawerlayout.widget.DrawerLayout>

TabLayout片段代码:单击底部导航选项卡时被替换。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".Fragments.HoMetab.HomeFragment">


    <com.google.android.material.tabs.TabLayout
        android:id="@+id/homeFragmentTabs"
        android:background="?attr/postCardBackground"
        app:tabTextColor="?attr/postUserNameColor"
        app:tabSelectedTextColor="?attr/toolbarTitleColor"
        app:tabIndicatorColor="?attr/toolbarTitleColor"
        app:tabRippleColor="?attr/toolbarTitleColor"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <androidx.viewpager.widget.ViewPager
        android:layout_below="@+id/homeFragmentTabs"
        android:id="@+id/homeFragmentViewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</RelativeLayout>

输出图像:

Shadow image

解决方法

问题已通过在AppBar布局中添加以下行来解决。无需设置海拔高度。

android:stateListAnimator =“ @ null”

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