在Android 2.2上,当使用较小的文本大小来渐变背景的按钮时,预期按钮会缩小,但在Android 4上并不是这样。我唯一能让它正常工作的方法是将按钮转换为TextView并在其上设置点击事件。我曾看到过有人提到TextView存在此问题,但对我而言,这些都按预期工作。
我曾在Android 2.2手机上看到它按预期工作,但在Android 4.0.4手机和Android 4.2.2模拟器上,按钮仍保持完整高度。
请告诉我可能出了什么问题。该示例使用Eclipse(Juno)中的新应用程序向导中的所有默认值。
注意:对于我的实际应用程序,我使用了
activity_main.xml:
我曾在Android 2.2手机上看到它按预期工作,但在Android 4.0.4手机和Android 4.2.2模拟器上,按钮仍保持完整高度。
请告诉我可能出了什么问题。该示例使用Eclipse(Juno)中的新应用程序向导中的所有默认值。
注意:对于我的实际应用程序,我使用了
android:padding="4dp",使TextView看起来很好(不影响Button),但对于此示例,我使用了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>