效果如下 (第二个按钮)
所有的按钮均在一个带有android:stretchColumns="*"
的TableLayout
的多个TableRow
下。下帖代码。
main.xml
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/gray_bg"
android:text="@string/calculator_multiply"
/>
gray_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<corners android:radius="5dp"/>
<item android:drawable="@drawable/gray_default" android:state_pressed="false"></item>
<item android:drawable="@drawable/gray_active" android:state_pressed="true"></item>
</selector>
gray_default.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<corners android:radius="5dp"/>
<solid android:color="@color/gray_default"/>
</shape>
gray_active.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<corners android:radius="5dp"/>
<solid android:color="@color/gray_active"/>
</shape>
This is related to the Theme set by your application. This will happen if it is android:Theme or android:Theme.Black
Solution 1: Change Theme to android:Theme.Holo or android:Theme.Holo.Light
Solution 2: Add the android:minHeight="48dp" attribute to Button
Because you have customized the button style, but it is not the same as the system default button style, but it overrides the default style. You can change other attributes in the shape to make it the same as before. We recommend an Android Button making artifact, what you see is what you get.
The essential reason is that the padding settings of different themes are different, so the minimum button height/width is different.