删除android.widget.Toolbar阴影

问题描述 投票:26回答:9

使用API​​21 + 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

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

java android android-styles
9个回答
68
投票

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

51
投票

使用属性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>

OUTPUT:

enter image description here

更新:

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

getSupportActionBar().setElevation(0);

希望这会有所帮助〜


19
投票

我自己找到了解决方案:

getActionBar().setElevation(0);

更多信息:

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


8
投票

而不是android:提升尝试app:提升

<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>

3
投票
findViewById(R.id.appBarLayout).bringToFront();

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

this question


0
投票

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


0
投票

将此代码放在Fragment中,您要隐藏ToolBar

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

0
投票

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


-1
投票
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  appBarLayout.outlineProvider = null
}
© www.soinside.com 2019 - 2024. All rights reserved.