android button 图标不居中
时间: 2023-07-28 22:01:29 浏览: 77
如果你想要让Android Button上的图标居中显示,可以尝试以下解决方案:
1. 在Button中设置android:gravity="center"属性,可以让Button中的文本和图标都居中显示。
2. 如果你只想让图标居中,可以使用android:drawablePadding属性来控制图标和文本之间的间距,例如:android:drawablePadding="8dp"。
3. 如果上述解决方案无效,可以尝试通过自定义Button的方式来实现。可以继承Button类,重写onDraw方法,手动绘制图标并将其居中显示。
希望这些方法能够帮助到你。
相关问题
Android studio button 怎样居中
要将Android Studio中的按钮居中,可以使用以下步骤:
1. 选中你的按钮控件,然后在右侧的“Attributes”(属性)选项卡中找到“layout_gravity”属性。
2. 将“layout_gravity”属性的值设置为“center”,这将使按钮在其容器中水平和垂直居中。
3. 如果你的按钮控件在一个布局容器中,例如LinearLayout或RelativeLayout,你还可以使用这些容器的其他属性来实现按钮的居中,例如:
- 在LinearLayout中,你可以将“gravity”属性设置为“center”。
- 在RelativeLayout中,你可以使用“android:layout_centerHorizontal”和“android:layout_centerVertical”属性将按钮水平和垂直居中。
希望这可以帮助你将按钮居中!
android button 带图标
要在 Android Button 上添加图标,可以使用以下两种方法:
1. 使用 Compound Drawables
在 Button 的 XML 布局文件中,可以使用 `android:drawableLeft`、`android:drawableTop`、`android:drawableRight`、`android:drawableBottom` 属性添加图标。例如,以下代码将在 Button 左边添加一个图标:
```
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button Text"
android:drawableLeft="@drawable/icon" />
```
2. 自定义 Button
可以自定义 Button 的样式来添加图标。首先,创建一个 XML 文件,定义 Button 的样式,例如以下代码:
```
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/button_pressed" android:state_pressed="true" />
<item android:drawable="@drawable/button_normal" />
</selector>
```
然后,在布局文件中使用自定义的样式:
```
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button Text"
android:background="@drawable/button_background"
android:drawableLeft="@drawable/icon" />
```
其中,`@drawable/button_background` 是定义 Button 样式的 XML 文件。需要注意的是,自定义样式需要在 `res/drawable` 目录下创建。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)