android 设置控件阴影
时间: 2023-08-18 08:08:31 浏览: 70
在Android中,可以使用以下两种方法为控件添加阴影:
1. 使用阴影属性
从Android 5.0(API级别21)开始,可以使用android:elevation属性为控件添加阴影。该属性指定控件的Z轴高度,高度越大,阴影越大。例如:
```
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:elevation="8dp"
android:text="Button" />
```
以上代码将为Button控件添加8dp的阴影。
2. 使用CardView
CardView是一个Material Design组件,可以为其内部的控件添加阴影。要使用CardView,需要在项目的build.gradle文件中添加以下依赖:
```
implementation 'androidx.cardview:cardview:1.0.0'
```
然后,在布局文件中使用CardView包裹需要添加阴影的控件,例如:
```
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="8dp"
app:cardCornerRadius="4dp">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</androidx.cardview.widget.CardView>
```
以上代码将为CardView及其内部的Button控件添加8dp的阴影,并且将CardView的圆角设置为4dp。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)