4
当在带有背景渐变的按钮上使用较小的文本大小时,该按钮会在Android 2.2上缩小,但不会在Android 4上缩小。唯一能让它工作的方法是将该按钮转换为TextView并设置点击它上面的事件。我已经看到提到TextViews有这个问题,但那些工作正如我所料。Android按钮顶部/底部填充不适用于Android 4?
我已经看到它在Android 2.2手机上按预期工作,但在Android 4.0.4手机和Android 4.2.2仿真器上保持全高的按钮。
请让我知道我可能做错了什么。本示例使用Eclipse(Juno)中新应用程序向导的所有默认值。
注意:对于我的真实应用程序,我使用的是使TextView看起来不错(不影响按钮)的
android:padding="4dp",但我在此示例中使用了0dp来夸大此问题。
activity_main.xml中:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button_gradient"
android:text="@string/hello_world" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:background="@drawable/button_gradient"
android:text="@string/hello_world" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="0dp"
android:textSize="12sp"
android:background="@drawable/button_gradient"
android:text="@string/hello_world" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="0dp"
android:textSize="12sp"
android:background="@drawable/button_gradient"
android:text="@string/hello_world" />
</LinearLayout>
button_gradient.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#0000BB"
android:endColor="#E0E0E0"
android:angle="90"/>
<corners android:radius="10dp" />
</shape>
AndroidManifest.xml中
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.test.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
感谢PiyushMishra。我尝试了你的建议,并采取相同的行动。我给每个元素一个id,并使用layout_below属性使元素像LinearLayout一样堆叠起来,这样我就可以看到它们,并且它们仍然覆盖在文本的上方和下方。 FWIW,在Eclipse(Juno)中查看布局时,我发现图形布局中显示的问题即使在Android 2.2上正常工作。 – Gravitoid 2013-05-28 14:14:15