Appcompat v21 Toolbar elevation pre-lollipop

后端 未结 10 558
你的背包
你的背包 2020-12-23 13:33

First off, I know that this question has been asked before, but it hasn\'t been answered before. I hope someone can give me an answer.

In my application, I use the T

相关标签:
10条回答
  • 2020-12-23 14:16

    You can add the shadow (elevation) back by using a FrameLayout with foreground="?android:windowContentOverlay". The elevation attribute is not supported pre-Lollipop. So if you are using FrameLayout like fragment container just add foreground attribute to it.

    0 讨论(0)
  • 2020-12-23 14:19

    As I've had issues with the CardView widget method, I've used the FrameLayout method as mentioned by @Sniper; it is working perfectly!

    I just wanted to share the code snippet you'll have to use. Just put this directly under the toolbar where your main content starts:

    <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:foreground="?android:windowContentOverlay">
    

    And don't forget to close with:

    </FrameLayout>
    
    0 讨论(0)
  • 2020-12-23 14:20

    To show shadow under your toolbar please use AppBarLayout available in Google Android Design Support Library. Here is an example of how it should be used.

    <android.support.design.widget.AppBarLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
       <android.support.v7.widget.Toolbar
                android:layout_height="?attr/actionBarSize"
                android:layout_width="match_parent"/>
         </android.support.design.widget.AppBarLayout>
    

    To use Google Android Design Support Library enter following into your build.gradle file:

     compile 'com.android.support:design:22.2.0'
    
    0 讨论(0)
  • 2020-12-23 14:23

    Using CardView container for toolbar is a bad idea.

    CardView is heavy, especially for low end devices.

    The best way is to put a gradient Shadow view below the toolbar. Shadow view must be a direct child to the coordinator layout. ie. The appbar which contains toolbar and shadow View must be siblings.

    Add this view component to your layout.

     <View
        android:id="@+id/gradientShadow"
        android:layout_width="match_parent"
        android:layout_height="5dp"
        android:background="@drawable/toolbar_shadow"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        app:layout_collapseMode="pin"/>
    

    The drawable toolbar_shadow.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">
    <gradient
        android:angle="90"
        android:endColor="#33333333"
        android:startColor="@android:color/transparent"/>
    </shape>
    

    This will solve the problems in pre-lollipop devices. But we don't want this shadow in lollipop and above devices so make visibility to gone in devices with lollipop and above.

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            findViewById(R.id.gradientShadow).setVisibility(View.GONE);
    }
    

    Done.

    0 讨论(0)
  • 2020-12-23 14:24

    Another option is the following library that provides 9patch shadows like the iosched app, android-materialshadowninepatch

    0 讨论(0)
  • 2020-12-23 14:26

    The solution with a view to add a shadow manually would work as long as there are no action bar menus. If so, the shadow view would stop before the action bar icons.

    i think it is easier to have a vertical linear layout with appbar on the top and a view for shadow below it as the next linear layout item or in my case, it is

    <LinearLayout Vertical> 
      <v7 toolbar/>
      <RelativeLayout>
         <View for shadow with alignParent_top= true/>
          ....
      </RelativeLayout>
    </LinearLayout>
    

    I really hope the near future appCompat would fix this.

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