如何在Android API中模拟低于21的按钮高程(阴影)?

Ram*_*iro 8 android android-studio material-design android-elevation

我只用一个按钮就可以实现这个最小的Android Studio项目.我为按钮指定了一个阴影:

android:elevation="3dp"
android:translationZ="3dp"
android:stateListAnimator="@null"
Run Code Online (Sandbox Code Playgroud)

我在Android Studio Design标签中看到了阴影.但我也在xml编辑器中收到警告

属性...仅用于API级别21及更高级别(当前最小值为16)

实际上,按钮阴影仅在模拟器中显示API level 21 or higher,并且显示为平面,在模拟器中没有阴影API level lower than 21.

所以具体问题是,如何模拟阴影效果API's lower than 21.

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <LinearLayout
        android:id="@+id/main_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:weightSum="1">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="20dp"
            android:layout_weight="2.24">

            <Button
                android:id="@+id/my_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_marginLeft="20dp"
                android:layout_marginBottom="20dp"
                android:elevation="5dp"
                android:translationZ="5dp"
                android:stateListAnimator="@null"
                android:background="@android:color/holo_green_light"
                android:text="BUTTON"/>

        </RelativeLayout>
    </LinearLayout>
</ScrollView>
Run Code Online (Sandbox Code Playgroud)

MainActivity.java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

}
Run Code Online (Sandbox Code Playgroud)

按钮显示阴影,在API 22: 在此输入图像描述

相同的按钮没有显示阴影,在API 16: 在此输入图像描述

Har*_*iya 10

对于Pre-LolliPop设备,您必须使用可绘制文件创建阴影或高程.这条路.

drawable在文件夹中创建文件,如element_background.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

<item>
    <shape android:shape="rectangle">
        <solid android:color="#BDBDBD"/>
        <corners android:radius="5dp"/>
    </shape>
</item>

<item
    android:left="0dp"
    android:right="0dp"
    android:top="0dp"
    android:bottom="2dp">
    <shape android:shape="rectangle">
        <solid android:color="#ffffff"/>
        <corners android:radius="5dp"/>
    </shape>
</item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)

然后将以下代码添加到您想要的元素.

android:background="@drawable/element_background"
Run Code Online (Sandbox Code Playgroud)

  • 是的@Ironman,它有效.但它需要大量的工作来处理按下按钮,聚焦等等.所以我要留下按钮外观,因为谷歌打算在每个API中使用它.谢谢! (4认同)

Zie*_*ony 6

如果您愿意,可以试试Carbon.它是一个库向后移植材料的功能,回到2.2.它还支持高程和生成的动画阴影.

https://github.com/ZieIony/Carbon