Android: I cannot remove elevation / shadow on my toolbar

前端 未结 10 2242
名媛妹妹
名媛妹妹 2020-12-08 01:57

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


        
相关标签:
10条回答
  • 2020-12-08 02:46

    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"
    
    0 讨论(0)
  • 2020-12-08 02:47

    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:

    0 讨论(0)
  • 2020-12-08 02:49

    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 e.g toolbar.setElevation(0);

    0 讨论(0)
  • 2020-12-08 02:52

    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"

    0 讨论(0)
提交回复
热议问题