我正在使用android studio,我尝试使用一些按钮在UI上做一些工作。
我为某些按钮创建了背景,但是当我在按钮上应用此背景时,按钮内的文本不会居中。
我认为按钮的边距保持不变,而应用的背景创建的小方块比按钮的边距小。我不知道如何使按钮的边距适合背景xml文件中编码的方形边缘。
这里是问题的图片:
这是后台代码(bg_rectangle_grey.xml):
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:width="@dimen/_34sdp" android:height="@dimen/_34sdp">
<shape android:shape="rectangle">
<stroke android:width="1dp" android:color="#ff6c7780" />
<corners android:radius="3dp" />
</shape>
</item>
</layer-list>
以下是带有按钮的xml文件的代码:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="@dimen/_15sdp"
android:style="?android:attr/buttonBarStyle">
<Button
android:style="?android:attr/buttonBarButtonStyle"
android:background="@drawable/bg_rectangle_grey"
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="M" />
我在谷歌上寻找答案,但它从来都不是我想要的。
如果有人可以提供帮助。 提前谢谢
答案 0 :(得分:0)
只需在按钮
中使用重力center
即可
<Button
android:style="?android:attr/buttonBarButtonStyle"
android:background="@drawable/bg_rectangle_grey"
android:id="@+id/button4"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="M" />
答案 1 :(得分:0)
请从下面的代码中删除android:width =“@ dimen / _34sdp”android:height =“@ dimen / _34sdp”,然后重试。
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:width="@dimen/_34sdp" android:height="@dimen/_34sdp">
<shape android:shape="rectangle">
<stroke android:width="1dp" android:color="#ff6c7780" />
<corners android:radius="3dp" />
</shape>
</item>
</layer-list>