按钮文字没有在按钮中垂直居中
问题描述:
我正在创建一个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());
是否有更改按钮中的文本的位置在哪里?
答
android:layout_gravity
定义其父项内部的布局对齐。要对齐你需要android:gravity
按钮中的文本,例如
<Button
...
android:gravity="center"
...>
</Button>
答
您的文本可能在字体中内置了一些填充作为每个字母的一部分,这就是为什么您的文本被推向一个方向。将layout_height设置为“wrap_content”,您的文本现在应该正确居中。
答
我发现最好使用9补丁图像为矩形背景,而不是xml drawable。
答
您的按钮太短正确适合文本。增加layout_height,文本应居中。
答
您可以将重心设置为中心:
android:gravity="center"