android

Android按钮如何设置阴影效果

小樊
115
2024-07-20 07:05:50
栏目: 编程语言

要在Android按钮上添加阴影效果,可以使用如下方法:

  1. 在XML布局文件中设置按钮的阴影效果:
<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button"
    android:background="@drawable/button_shadow"/>
  1. 创建一个名为button_shadow.xml的drawable资源文件,定义按钮的阴影效果:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#FFFFFF"/>
    <corners android:radius="8dp"/>
    <padding android:left="8dp" android:top="8dp" android:right="8dp" android:bottom="8dp"/>
    <size android:width="200dp" android:height="60dp"/>
    <stroke android:width="1dp" android:color="#B3B3B3"/>
    <gradient
        android:startColor="#E5E5E5"
        android:endColor="#FFFFFF"
        android:angle="270"/>
</shape>
  1. 在Activity中设置按钮的阴影效果:
Button button = findViewById(R.id.button);
button.setElevation(8f);

通过以上步骤,您可以在Android按钮上添加阴影效果。您可以根据需要调整阴影的颜色、大小和位置来实现不同的效果。

0
看了该问题的人还看了