Appcompat v21 Toolbar elevation pre-lollipop

后端 未结 10 560
你的背包
你的背包 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:27

    I'm using this answer:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    
    <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/orange"
        android:titleTextAppearance="@color/White"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
    
        <View
            android:layout_width="match_parent"
            android:layout_height="5dp"
            android:background="@drawable/toolbar_shadow" />
    </LinearLayout>
    

    toolbar_shadow.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
        <gradient
            android:startColor="#3f3f3f"
            android:endColor="@android:color/transparent"
            android:angle="270" />
    </shape>
    
    0 讨论(0)
  • 2020-12-23 14:31

    It's possible to have real shadows - animated and generated. The method used by Lollipop is available since Froyo. Hardware acceleration used for shadow generation is available since Honeycomb I guess. Here's how it works:

    • draw your view to an off-screen bitmap with LightingColorFilter set to 0,0
    • blur the black shape (the off-screen bitmap) using the ScriptIntrinsicBlur class and elevation value as radius
    • draw the bitmap beneath the view

    It requires adding custom elevation attributes, custom views capable of rendering shadows, and using render script and the compatibility library (for older devices). I'm not going to dive into the details, because there's a lot of them including issues with compilation and minor performance optimisations. But it's possible.

    Why there's no shadows in the official support library?

    • it would require changes in the UI framework as it's impossible to freely draw outside view bounds
    • smooth animation requires a quite good GPU

    See:

    • https://www.youtube.com/watch?v=jbU4SXblO5s
    • https://androidreclib.wordpress.com/2014/11/25/lollipops-shadows-on-gingerbread/
    • https://github.com/ZieIony/Carbon
    0 讨论(0)
  • 2020-12-23 14:31

    You can't use the elevation attribute before API 21 (Android Lollipop). You can however add the shadow programmatically, for example using a custom view placed below the Toolbar.

    For example:

    <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/toolbar"
            android:background="@drawable/shadow"/>
    

    Where the shadow is a drawable with a black gradient.

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

    This worked for me very well:

    <android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/primary"
        card_view:cardElevation="4dp"
        card_view:cardCornerRadius="0dp">
    
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/primary"
            android:minHeight="?attr/actionBarSize" />
    
    </android.support.v7.widget.CardView>
    
    0 讨论(0)
提交回复
热议问题