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

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

概述我只用一个按钮就可以实现这个最小的 Android Studio项目. 我为按钮指定了一个阴影: android:elevation="3dp"android:translationZ="3dp"android:stateListAnimator="@null" 我在Android Studio Design标签中看到了阴影.但我也在xml编辑器中收到警告 Atribute … is only 我只用一个按钮就可以实现这个最小的 Android Studio项目.
我为按钮指定了一个阴影:
androID:elevation="3dp"androID:translationZ="3dp"androID:stateListAnimator="@null"

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

Atribute … is only used in API level 21 and higher (current min is 16)

实际上,按钮阴影仅在API级别为21或更高的模拟器中显示,并且显示为平面,在API级别低于21的模拟器中完全没有阴影.

所以具体问题是,如何在API低于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>

MainActivity.java

public class MainActivity extends AppCompatActivity {    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.activity_main);    }}

在API 22中显示阴影的按钮:

在API 16中,相同的按钮不显示阴影:

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

在您的文件夹中创建可绘制文件,如element_background.xml

<?xml version="1.0" enCoding="utf-8"?><@R_945_3419@ 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></@R_945_3419@>

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

androID:background="@drawable/element_background"
总结

以上是内存溢出为你收集整理的如何在Android API中模拟低于21的按钮高程(阴影)?全部内容,希望文章能够帮你解决如何在Android API中模拟低于21的按钮高程(阴影)?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1133633.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存