android使用.9图作为背景,内容不能居中的问题解决方案

在xml中使用.9图作为背景,内容不能居中,试了好多方法最后,加一个属性就ok了。

android:padding:0dip;

解析:.9图作为背景时,不可拉伸的部分就相当于该空间的padding距离

我的这个.9图只是左边可以拉伸的,右边大概80px是固定的,不可拉伸的。所以就相当于右边设置了padding距离。

我们也不能设置一个反向的padding,因为app需要安装在不同分别率的设备上,所以这个值又是不固定的。

<Button
android:id="@+id/button_1"
style="?android:attr/buttonBarStyle"
android:layout_width="fill_parent"
android:layout_height="63dip"
android:layout_marginTop="15dip"
android:background="@drawable/newi_bg"
android:gravity="center"
android:padding="0dip"
android:text="@string/button_text"
android:textColor="#707070"
android:textSize="18sp" />

OK!

原文地址:https://www.cnblogs.com/yuan1225/p/4449431.html