2013-03-21 124 views
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> 

回答

0

而不是使用的LinearLayout可以使用的RelativeLayout的。

<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" /> 

+0

感謝PiyushMishra。我嘗試了你的建議,並採取相同的行動。我給每個元素一個id,並使用layout_below屬性使元素像LinearLayout一樣堆疊起來,這樣我就可以看到它們,並且它們仍然覆蓋在文本的上方和下方。 FWIW,在Eclipse(Juno)中查看佈局時,我發現圖形佈局中顯示的問題即使在Android 2.2上正常工作。 – Gravitoid 2013-05-28 14:14:15

相關問題