简体   繁体   English

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

[英]Android Button Top/Bottom Padding not working in Android 4?

When using a smaller text size on a button with a background gradient the button shrinks as expected on Android 2.2 but not on Android 4. The only way I could get it to work is to turn the button into a TextView and set up a click event on it. 当在具有背景渐变的按钮上使用较小的文本大小时,按钮在Android 2.2上按预期缩小,但在Android 4上不会缩小。我能使其工作的唯一方法是将按钮转换为TextView并设置单击事件在上面。 I have seen mention of TextViews having this issue but those work as expected for me. 我已经看到提到TextViews有这个问题,但那些工作正如我所料。

I have seen it working as expected on an Android 2.2 phone, but the button remaining full height on an Android 4.0.4 phone and on an Android 4.2.2 emulator. 我看到它在Android 2.2手机上按预期工作,但按钮在Android 4.0.4手机和Android 4.2.2仿真器上保持全高。

Please let me know what I might be doing wrong. 请让我知道我可能做错了什么。 This example uses all the defaults from the new app wizard in Eclipse (Juno). 此示例使用Eclipse(Juno)中新应用程序向导的所有默认值。

Note: for my real app I was using 注意:对于我使用的真实应用程序

android:padding="4dp"
which made the TextView look nice (didn't affect the Button) but I used 0dp for this example to exaggerate the issue. 这使得TextView看起来很漂亮(不影响Button),但我在这个例子中使用了0dp来夸大问题。

activity_main.xml: activity_main.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>

button_gradient.xml button_gradient.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>

AndroidManifest.xml 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> 

Instead of using LinearLayout you can use RelativeLayout. 您可以使用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" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android:ListView项目中文本的顶部和底部填充 - Android: Top and bottom padding of text in listview item Android:TextView:删除顶部和底部的间距和填充 - Android: TextView: Remove spacing and padding on top and bottom 具有默认顶部和底部填充的 Android 应用 - Android app with default top & bottom padding 使用TextView在顶部和底部进行Android不需要的填充 - Android unwanted padding on top and bottom with TextView Android ScrollView在子imageview的顶部和底部添加了额外的填充 - Android ScrollView adds extra padding on top and bottom of child imageview 如何删除Android复选框的内部图像填充(顶部,左侧和底部) - How to remove internal Image padding (Top, Left and Bottom) of an Android Checkbox Android 4 - LinearLayout顶部和底部填充不起作用 - Android 4 - LinearLayout Top and Bottom padding doesn't work 如何设置或删除android SearchView的顶部和底部填充? - How to set or remove android SearchView top and bottom padding? 如何从android的Bottom表中删除顶部填充? - How to remove top padding from Bottom sheet in android? Android:添加底部/顶部填充后,ScrollView不滚动 - Android: ScrollView is not scrolling after adding padding bottom/top
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM