2013-03-15 31 views
0

我有一個帶背景可繪製的Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"。按鈕上沒有文字。然而,它的顯示尺寸是這樣的,drawable被調整大小一點,看起來很模糊,除非我將width和height設置爲px中drawable的寬度和高度。我如何使wrap_content工作?或者其他任何不涉及硬編碼按鈕大小的方式,以便在drawable的大小更改時不需要進行其他編輯?設置背景圖像,使其不會調整大小(像素完美)

這裏是我的XML:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" xmlns:tools="http://schemas.android.com/tools"> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" > 

     <RelativeLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:paddingBottom="3dp" 
      android:paddingTop="3dp" > 

      <Button 
       android:id="@+id/btn" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:background="@drawable/btn" /> 

     </RelativeLayout> 
    </LinearLayout> 

</ScrollView> 
+0

把它放在'-nodpi'文件夾中。 – 323go 2013-03-15 15:18:11

+0

@ 323go:這將打破我的想法,有幾個版本的可繪製 - 例如hdpi的64x64和xhdpi的128x128,並讓Android自動選擇最佳版本。 – 2013-03-15 15:20:57

+0

您可以根據自己的分辨率選擇正確的版本...但我不知道是否發現自己恢復到允許Android選擇(和縮放)。 – 323go 2013-03-15 16:04:38

回答

1

使用的ImageButton而不是常規按鈕。它提供對酒店android:src的訪問,並在那裏設置您的圖像,而不是作爲背景。背景圖像始終適合填充,其中「源」屬性通過android:scaleType屬性控制圖像大小調整。請參閱:http://developer.android.com/reference/android/widget/ImageButton.htmlhttp://developer.android.com/reference/android/widget/ImageView.html#attr_android:scaleType

注意,爲您的ImageButton,你還需要確保android:background="@null",否則,你會看到你的源圖像背後的默認按鈕圖像。

編輯:

這是您的XML的更新版本。我已經添加了一個新按鈕,在您開始使用的按鈕之上,儘管我使用了其中一張圖片。特定的圖像很長,而且不那麼高。在常規按鈕中,它延伸以填充小部件,失去了透視。在新的ImageButton頂部,它保持了它的視角。

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" xmlns:tools="http://schemas.android.com/tools"> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" > 

     <RelativeLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:paddingBottom="3dp" 
      android:paddingTop="3dp" > 

      <ImageButton 
       android:id="@+id/new_btn" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:background="@null" 
       android:scaleType="fitCenter" 
       android:src="@drawable/postwall" /> 

      <Button 
       android:id="@+id/btn" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_below="@id/new_btn" 
       android:background="@drawable/postwall" /> 

     </RelativeLayout> 
    </LinearLayout> 

</ScrollView> 
+0

謝謝,不知道這個。但是如果我需要一個「ToggleButton」呢? – 2013-03-18 12:32:04

+0

此外,您的方法不起作用。我照你說的做了(用帶有建議參數的ImageButton代替了常規Button),並且它仍然被拉伸。 – 2013-03-18 12:50:47

+0

它工作正常,因爲我在許多應用程序中使用過這種方法。你確定你把圖片放在SOURCE('src')位置而不是BACKGROUND位置嗎?背景中的任何圖像都會被拉伸。 Button和ImageButton的區別在於只有ImageButton具有Source選項。 – 2013-03-18 16:01:20