Android 4中的按钮顶部/底部填充不起作用?

4
在Android 2.2上,当使用较小的文本大小来渐变背景的按钮时,预期按钮会缩小,但在Android 4上并不是这样。我唯一能让它正常工作的方法是将按钮转换为TextView并在其上设置点击事件。我曾看到过有人提到TextView存在此问题,但对我而言,这些都按预期工作。
我曾在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>
1个回答

0

你可以使用RelativeLayout代替LinearLayout。

<RelativeLayout 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:padding="20dp"
android:layout_margin="10dp"
android:orientation="vertical"
tools:context=".MainActivity" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textView1"
    android:gravity="center"
    android:layout_alignLeft="@+id/button1"
    android:layout_alignRight="@+id/button1"
    android:text="@string/hello_world" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/button1"
    android:layout_below="@+id/textView1"
    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:id="@+id/button2"
    android:layout_below="@+id/button1"
     android:layout_alignLeft="@+id/button1"
    android:layout_alignRight="@+id/button1"
    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:id="@+id/button3"
     android:layout_alignLeft="@+id/button1"
    android:layout_alignRight="@+id/button1"
    android:layout_below="@+id/button2"
    android:background="@drawable/button_gradient"
    android:text="@string/hello_world" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:textSize="12sp"
    android:layout_below="@+id/button3"
    android:layout_alignLeft="@+id/button1"
    android:layout_alignRight="@+id/button1"
    android:background="@drawable/button_gradient"
    android:text="@string/hello_world" />


谢谢PiyushMishra。我尝试了你建议的方法,但结果还是一样。我给每个元素分配了一个ID,并使用layout_below属性将它们堆叠起来,就像LinearLayout一样,这样我就可以看到它们,但它们仍然在文本上方和下方过度填充。顺便说一下,在Eclipse(Juno)中查看布局时,我在图形布局中看到了问题的表现,即使在Android 2.2上它也能正常工作。 - Gravitoid

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接