2012-02-15 14 views
5

我正在嘗試設計一個單選按鈕的樣式,所以我創建了一個選擇器。除了標準的單選按鈕圖像仍然顯示以外,一切正常。我如何刪除默認的單選按鈕複選框。我試圖將drawable分配給一個空字符串,但編譯器不喜歡它。使用自定義選擇器時刪除默認的單選按鈕複選框

這是我選擇的國家之一的代碼:

<item android:state_checked="true" android:drawable=""> 
    <shape> 
     <gradient 
      android:startColor="#808080" 
      android:centerColor="#2F2F2F" 
      android:endColor="#2F2F2F" 
      android:angle="270" /> 
     <stroke 
      android:width="2dp" 
      android:color="@color/black" /> 
     <corners 
      android:radius="5dp" /> 
    </shape> 
</item> 
+1

你爲什麼試圖刪除繪製一起?你可以用適合你需求的東西來替代它嗎? – Flynn 2012-02-15 17:28:48

+0

我想要一個單選按鈕的功能,但希望它看起來像一個按鈕。具體而言,我想有兩個並排切換的按鈕。其中一個按鈕出現了,另一個出現了。有沒有更好的解決方案比我要去的地方更好? – 2012-02-15 19:13:53

+2

我居然發現我可以用下面的'android:button =「@ null」' – 2012-02-15 21:20:30

回答

19

我發現我不能刪除默認單選按鈕複選框中選擇。我發現它可以通過將「button」屬性分配給null,在樣式或單選按鈕本身的定義中刪除。

以下是使用樣式的示例。

<RadioButton 
    android:id="@+id/myRadioButton" 
    style="@style/RadioButtonStyle" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/custom_radio_button"/> 

<style name="RadioButtonStyle" parent="@android:style/Widget.CompoundButton"> 
    <item name="android:background">@drawable/radiobutton_selector</item> 
    <item name="android:button">@null</item> 
</style> 

radiobutton_selector限定繪製在其不同檢查狀態下的按鈕的選擇器。這裏是選中狀態:

<item android:state_checked="true"> 
    <shape> 
     <gradient 
      android:startColor="@color/titleButtonCheckedGradientLight" 
      android:centerColor="@color/titleButtonCheckedGradientDark" 
      android:endColor="@color/titleButtonCheckedGradientDark" 
      android:angle="270" /> 
     <stroke 
      android:width="1dp" 
      android:color="@color/titleButtonBorder" /> 
     <corners 
      android:radius="5dp" /> 
    </shape> 
</item> 
+0

刪除默認複選框,你的代碼似乎並沒有得到radioGroup.getCheckedRadioButtonId()函數......... – Prativa 2012-12-18 12:12:39

+0

最佳答案到目前爲止 – Devon 2017-08-01 13:12:58

26

設置按鈕值設置爲null在佈局XML

android:button="@null" 
+3

感謝您的回答。我從我的問題中看到2012年2月15日的評論,我也發現了這一點。 – 2013-07-25 19:33:40