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

前端之家收集整理的这篇文章主要介绍了android – 按钮文本没有在按钮中垂直居中前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在创建一个 Android应用程序,我有一个按钮,后面有自定义背景图片.背景图像只是一个矩形,但现在按钮文本似乎没有垂直居中.这是我的按钮xml:
  1. <Button
  2. android:id="@+id/save_to_list"
  3. android:layout_below="@+id/info_pic"
  4. android:text="SAVE TO LIST"
  5. android:textColor="@android:color/white"
  6. android:layout_centerHorizontal="true"
  7. android:layout_gravity="center_horizontal"
  8. android:layout_width="260dp"
  9. android:layout_height="35dp"
  10. android:layout_marginTop="15dp"
  11. android:layout_marginBottom="15dp"></Button>

我在这里设置背景:

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

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

解决方法

android:layout_gravity定义了它的父级内部的布局对齐.要对齐按钮中的文本,您需要android:gravity,例如
  1. <Button
  2. ...
  3. android:gravity="center"
  4. ...>
  5. </Button>

猜你在找的Android相关文章