How to show the shadow of the ActionBar&Toolbar of the support library on all Android versions?

后端 未结 2 465
旧巷少年郎
旧巷少年郎 2020-12-08 11:22

This is a simple question:

I use the new support library, which allows me to have a Toolbar instance and even set it as the actionBar of the activity (or use the def

相关标签:
2条回答
  • 2020-12-08 12:03

    This is what I have done in one of my apps, it's showing shadow (artificial shadow) :

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/mainActivityLinearLayout"
            android:layout_width="match_parent"
            android:orientation="vertical"
            android:layout_height="match_parent"
            android:background="@drawable/logo">
    
          <LinearLayout
            android:layout_height="?attr/actionBarSize"
            android:layout_width="match_parent"
            android:orientation="vertical">
    
            <!-- refer to your toolbar layout -->
            <include layout="@layout/toolbar"/>
    
          </LinearLayout>
    
          <!-- My Left Drawer -->
          <android.support.v4.widget.DrawerLayout
            android:id="@+id/drawer_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"    >
    
            <RelativeLayout
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/relative_layout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginStart="0dp"
                android:layout_marginLeft="0dp" >
    
               ....
    
               <!-- Lots of other stuffs -->             
    
               ....
    
               <!-- Shadow -->
              <View
               android:layout_width="match_parent"
               android:layout_below="@+id/toolbarShadow"
               android:layout_marginTop="0dp"
               android:layout_height="6dp"
               android:background="@drawable/toolbar_dropshadow" />
    
            </RelativeLayout>
         </android.support.v4.widget.DrawerLayout>
    
    
     </LinearLayout>
    

    toolbar_dropshadow.xml :

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape>
            <gradient
                android:angle="90"
                android:startColor="@android:color/transparent"
                android:endColor="#D6D6D6"
                android:type="linear" />
        </shape>
    </item>
    </selector>
    

    I am using a map activity and all this is showing wonderful shadow on my map view.

    Hope this helps...

    0 讨论(0)
  • 2020-12-08 12:04

    As far as I know/can read, the AppCompat library currently does not render any shadows. The calls seem to be there (e.g. ViewCompat.setElevation(View view, float elevation)) but it doesn't seem to be doing anything.

    So I guess you will need to resort to adding your own shadows for now, at least until the support library implements the shadow rendering.

    Looking at the Google IO 2014 source code, they have implemented like this:

    • DrawShadowFrameLayout which can render a background drawable as a shadow
    • bottom_shadow.9.png which represents a shadow below an actionbar
    • app:shadowDrawable attribute to set the shadow drawable
    • setShadowTopOffset() on the DrawShadowFrameLayout shifts the shadow down so it appears nicely below the actionbar. (you need to set this yourself)

    More information on this Reddit Thread and stackoverflow.

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