删除android.widget.Toolbar阴影

时间:2017-04-06 12:39:48

标签: java android android-styles

使用API​​21 + Toolbar

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

想彻底删除它的影子。由于setElevation(0)已经返回getElevation(),因此0无法执行任何操作。

有Material Design参考:

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

有开发参考:

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

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

问题:如何完全删除/隐藏Toolbar影子?

10 个答案:

答案 0 :(得分:65)

在工具栏上使用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>

答案 1 :(得分:48)

将属性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);

希望这会有所帮助〜

答案 2 :(得分:18)

我自己找到了解决方案:

getActionBar().setElevation(0);

更多信息:

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

答案 3 :(得分:8)

Instaed of android:elevation try 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>

答案 4 :(得分:3)

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

为我工作版本问题我想谢谢Bjorn

this question

中的

答案 5 :(得分:0)

如果您在工具栏中使用AppbarLayout,则可以在AppBar布局中使用app:elevation =“ 0dp”代替android:elevation

答案 6 :(得分:0)

将此代码放在您要隐藏Fragment的{​​{1}}中。

ToolBar

答案 7 :(得分:0)

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

答案 8 :(得分:0)

当你在 appbarlayout 上添加高程 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>

答案 9 :(得分:-1)

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