删除 android.widget. 工具栏阴影

使用 API21 + Toolbar:

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

想要完全移除它的阴影。 setElevation(0)不做任何事情,因为 getElevation()已经返回 0

材料设计参考:

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

参考文献:

Https://developer.android.com/reference/android/widget/toolbar.html

但我没看到任何与影子有关的信息

问: 如何完全去除/隐藏 Toolbar阴影?

58900 次浏览

我自己也找到了解决办法:

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>

产出:

enter image description here

更新:

如果要以编程方式删除它,可以使用以下代码:

getSupportActionBar().setElevation(0);

希望这能有所帮助 ~

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

为我工作的版本问题,我认为感谢 比约恩

这个问题

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

如果在工具栏中使用 AppBarLayout,可以使用: app:elevation="0dp" 而不是 AppBarLayout 中的 android:elevation

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

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

将“ # 0000000”设置为工具栏的背景色, 记住: 如果使用坐标布局和 AppBarLayout,则应删除 应用程序: lay_ action = “@string/appbar _ scrolling _ view _ actions” 我犯了这个基本的错误。

当您添加高度0dp 在您的应用程序布局,工具栏将完全消失。 因此,您还应该在 xml 中添加 transationZ。

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>