按钮文本没有在按钮中垂直居中

时间:2011-10-27 20:36:22

标签: android xml button

我正在创建一个Android应用,我有一个按钮,背后有自定义背景图片。背景图像只是一个矩形,但现在按钮文本似乎没有垂直居中。这是按钮的xml:

<Button 
android:id="@+id/save_to_list"
android:layout_below="@+id/info_pic"
android:text="SAVE TO LIST"
android:textColor="@android:color/white"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal"
android:layout_width="260dp"
android:layout_height="35dp"
android:layout_marginTop="15dp"
android:layout_marginBottom="15dp"></Button>

我在这里设置背景:

ImageView button_bg = (ImageView)findViewById(R.id.button_background);
savetolist_button.setBackgroundDrawable(button_bg.getDrawable());

是否有更改按钮内文本位置的位置?

4 个答案:

答案 0 :(得分:12)

android:layout_gravity在其父级内部定义布局对齐。要在按钮中对齐文本,您需要android:gravity,例如

<Button 
    ...
    android:gravity="center"
    ...>
</Button>

答案 1 :(得分:4)

您的按钮太短,无法正确放入文字。增加layout_height,文本应居中。

答案 2 :(得分:1)

您的文字可能在字体中嵌入了一些填充作为每个字母的一部分,这就是为什么您的文字被向一个方向推送的原因。将layout_height设置为“wrap_content”,您的文本现在应该正确居中。

答案 3 :(得分:0)

我发现最好将9补丁图像用于矩形背景而不是xml drawable。