简体   繁体   中英

Android: I cannot remove elevation / shadow on my toolbar

Hi i would like to remove the elevation and shadow effect from my toolbar for API 21 and greater. Below is what i have tried

setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        getSupportActionBar().setElevation(0);

My Toolbar in XML. I have tried to set elevation to 0dp

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    android:minHeight="?attr/actionBarSize"
    android:elevation="0dp"
    android:theme="@style/ToolbarStyle"
    app:theme="@style/ToolbarStyle"
    >

And this is ToolbarStyle if it helps

<style name="ToolbarStyle" parent="ThemeOverlay.AppCompat.Dark">
    <item name="colorControlNormal">@android:color/white</item>
</style>

Edit 1: Tried the following in styles v21. Still same result

<style name="ToolbarStyle" parent="ThemeOverlay.AppCompat.Dark">
    <item name="colorControlNormal">@android:color/white</item>
    <item name="android:elevation">0dp</item>
</style>

Edit 2: Where the toolbar is in my layout

android.support.v4.widget.DrawerLayout 

    xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.CoordinatorLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

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

                <include layout="@layout/toolbar" />

You can do that with an easy step. Don't remove the android.support.design.widget.AppBarLayout from your layout, instead add the attribute app:elevation="0dp" to it. Your final layout will be:

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

This remove elevation on toolbar (android:stateListAnimator="@null"), stateListAnimator is for API level 21 or higher, so insert a tools prop (before namespace declaration) like this:

//xmlns:tools="http://schemas.android.com/tools" in parent or start element

     <android.support.design.widget.AppBarLayout
        android:id="@+id/appBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:stateListAnimator="@null" 
        android:theme="@style/AppTheme.AppBarOverlay"
        tools:targetApi="21">

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

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

//your content goes here like RecyclerView, Card or other layout 

Here is my sample result:

在此处输入图片说明

Use setOutlineProvider(null); on your appBar. Just be sure to check the SDK version. :)

Use app:elevation="0dp" in your AppBarLayout. Make sure you put Toolbar inside your AppBarLayout like this -

    <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/AppTheme.AppBarOverlay"
        android:fitsSystemWindows="true"
        app:elevation="0dp"> 

        <android.support.v7.widget.Toolbar
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/firstOption"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

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

and add this line to your activity - findViewById(R.id.appBarLayout).bringToFront();
This will bring the AppBar to front.

Just remove this part of you layout hierarchy:

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

It's responsible for creating shadow. Toolbar itself doesn't cast any shadow.

Go to the activity you want to remove the ActionBar's Elevation. Before setContent(....), request the ActionBar feature(That is if you have not declared it directly)

    getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
    getSupportActionBar().setElevation(0);

or else just call the declared Toolbar variable eg toolbar.setElevation(0);

Get the appbarlayout in code:

var appbarLayout = findviewbyid<AppBarLayout>(resource.id.  );  

set the property

appbarLayout.StateListAnimator=null;

Create another style for v21 and add

<item name="android:elevation">@dimen/toolbar_elevation</item> to that style. On normal style don't use elevation and don't use it in xml, just use style="@style/yourCustomToolbarStyle"

Solution is simple. Just add following line into your AppBar layout. No need to add elevation for the Toolbar or AppBar layout after this.

android:stateListAnimator="@null"

use android:outlineSpotShadowColor=" assign transparent colour" your shadow will disappear its work for my as below

enter image description here

要使用 java 代码删除高程,请使用以下行...

getSupportActionBar().setElevation(0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM