删除 android.widget.Toolbar 阴影

Remove android.widget.Toolbar shadow

使用 API21+ Toolbar:

// Toolbar
Toolbar toolbar = new Toolbar(this);
toolbar.showOverflowMenu();

想彻底去除它的影子。 setElevation(0) 什么都不做,因为 getElevation() 已经 returns 0.

有Material设计参考:

https://material.io/guidelines/layout/structure.html#structure-toolbars

有开发参考:

https://developer.android.com/reference/android/widget/Toolbar.html

但是我没有看到任何与影子相关的信息。 Toolbar

问题:如何remove/hideToolbar阴影完全?

我自己找到了解决方案:

getActionBar().setElevation(0);

更多信息:

https://developer.android.com/reference/android/app/Activity.html#getActionBar()

在工具栏上使用 app:elevation="0dp" 而不是 android:elevation。 如果不起作用,请将工具栏放在 AppBarLayout 内并设置 app:elevation="0dp":

<android.support.design.widget.AppBarLayout
            android:id="@+id/appBarLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:elevation="0dp">
            ...
</android.support.design.widget.AppBarLayout>

而不是 android:elevation 试试 app:elevation:

<android.support.design.widget.AppBarLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"       
    app:elevation="0dp">
</android.support.design.widget.AppBarLayout>

将属性 app:elevation="0dp" 用于 ToolbarAppBarLayout 以移除阴影。

#. 如果您只使用 Toolbar,则将属性 app:elevation="0dp" 添加到 Toolbar.

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

#. 如果您使用 AppBarLayout 作为 Toolbar 的容器,则将属性 app:elevation="0dp" 添加到 AppBarLayout .

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay"
        app:elevation="0dp">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

输出:

更新:

如果你想以编程方式删除它,那么你可以使用下面的代码:

getSupportActionBar().setElevation(0);

希望对你有所帮助~

findViewById(R.id.appBarLayout).bringToFront();

我认为版本问题对我有用谢谢Bjorn

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  appBarLayout.outlineProvider = null
}

如果您在工具栏中使用AppBarLayout,您可以使用: app:elevation="0dp" 代替 android:elevation 在 AppBarLayout

将此代码放入要隐藏 ToolBarFragment 中。

@Override
public void onResume() {
    super.onResume();
    ((AppCompatActivity)getActivity()).getSupportActionBar().setElevation(0);
}

将“#00000000”设置为工具栏的背景色, 请记住: 如果你使用 CoordinatorLayout 和 AppBarLayout ,你应该删除 app:layout_behavior="@string/appbar_scrolling_view_behavior" 你的 contentView ,我犯了这个基本错误。

当你在你的应用程序栏布局上添加海拔0dp时,工具栏将完全消失。 这样您还应该在 xml.

中添加 translationZ
android:translationZ="0.1dp"
app:elevation="0dp"

看起来会像 ->

  <com.google.android.material.appbar.AppBarLayout
    android:id="@+id/toolbarLayout"
    android:background="@android:color/transparent"
    android:translationZ="0.1dp"
    app:elevation="0dp"
    android:theme="@style/ToolbarTheme"
    >

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
       />
</com.google.android.material.appbar.AppBarLayout>