安卓button设置图标,并且居右显示,并且设置图标设配button的高度
时间: 2024-05-14 14:12:24 浏览: 9
在XML布局文件中,可以通过以下代码来设置Button的图标,并使其居右显示:
```
<Button
android:id="@+id/my_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableRight="@drawable/my_icon"
android:gravity="right|center_vertical"
android:text="My Button" />
```
在上述代码中,`android:drawableRight`属性指定了Button的图标资源,`android:gravity`属性将文本和图标居中对齐,并将图标放置在文本的右侧。
为了使图标适应Button的高度,可以使用以下代码:
```
<Button
android:id="@+id/my_button"
android:layout_width="match_parent"
android:layout_height="48dp"
android:drawableRight="@drawable/my_icon"
android:gravity="right|center_vertical"
android:text="My Button"
android:paddingRight="8dp" />
```
在上述代码中,`android:layout_height`属性指定了Button的高度为48dp,`android:paddingRight`属性将图标与文本之间的间距设置为8dp。这样,图标就会根据Button的高度进行缩放。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)