2009-02-10 83 views
4

我已經使用XML佈局創建器在LinearLayout(下面的XML中包含的XML)中使用RadioGroup在Android中創建了自定義圖標欄。 RadioGroup中的每個RadioButton都有一個自定義可繪製(帶有選中和未選中狀態)作爲其android:button屬性。如何使用XML根據圖像大小調整Android組件的大小

線性佈局本身在RelativeLayout之內,因此它可以出現在屏幕上的任意位置(在本例中爲側欄)。

繪圖寬55像素,所有這些視圖的android:layout\_width屬性是wrap\_content

當我使這個組件可見時,與屏幕的左下角對齊,只有大約四分之三的RadioButton圖像寬度可見。將RelativeLayoutlayout\_width設置爲fill\_parent可解決此問題並導致出現完整按鈕。

但是這意味着按鈕組消耗整個屏幕寬度的點擊事件。

如何讓整個按鈕顯示,並且只有按鈕的區域響應點擊?對drawable的寬度進行硬編碼並不是特別理想的解決方案。

<RelativeLayout android:id="@+id/RelativeLayout01" android:layout_width="wrap_content" android:layout_height="wrap_content"> 
    <LinearLayout android:id="@+id/LinearLayout02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_below="@+id/LinearLayout01" android:visibility="invisible"> 
     <RadioGroup android:id="@+id/RadioGroup01" android:layout_height="wrap_content" android:checkedButton="@+id/RB01" android:layout_width="fill_parent"> 
      <RadioButton android:id="@+id/RB01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:button="@drawable/DR01"/> 
      <RadioButton android:id="@+id/RB02" android:layout_width="fill_parent" android:layout_height="wrap_content" android:button="@drawable/DR02"/> 
      <RadioButton android:id="@+id/RB03" android:layout_width="fill_parent" android:layout_height="wrap_content" android:button="@drawable/DR03"/> 
      <RadioButton android:id="@+id/RB04" android:layout_width="fill_parent" android:layout_height="wrap_content" android:button="@drawable/DR04"/> 
     </RadioGroup> 
    </LinearLayout> 
    </RelativeLayout> 

回答

7

設置的LinearLayout,RadioGroup中,或單選按鈕的DP的(密度indpendent像素)的寬度可能爲你工作。根據你上面所說的,我認爲它必須是佈局的寬度。尺寸是「硬編碼」的,但它們會隨着屏幕的大小而擴展。

因此XML可能看起來像:

android:layout_width="55dp" 

對DPS的官方文檔是here

+2

爲什麼55 dp?所有Android的寬度爲220dp? – Jaseem 2011-12-30 11:48:28

1

我無法想象爲什麼其他的答案有6個upvotes,這是完全錯誤的

這些尺寸是「硬編碼」的,但它們會隨着屏幕的大小而變化。

沒有他們將規模根據你的屏幕密度。我非常希望我們可以使用XML中的百分比,而不必編寫它們。

+0

'因爲它工作;) - 一個可見的變化! – 2013-01-03 16:27:24

相關問題