2017-10-12 132 views
1

我試圖改變RadioButton上的選中按鈕的drawable,但沒有任何顯示;設置drawable只是刪除已經存在的默認值。RadioButton'android:button'什麼也不做

我使用堆棧溢出來嘗試找到解決方案,但是當我嘗試實現它時,沒有可繪製的顯示。我的選擇器似乎很好,因爲當我設置顯示我的圓圈的單選按鈕的background

我敢肯定,這是一個非常明顯的問題,我會因爲看不見而自拍;誰能弄清楚什麼是錯的?

我的單選按鈕

<RadioGroup 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 
     <RadioButton 
      android:id="@+id/radio0" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Left" 
      android:checked="false" 
      android:button="@drawable/radio_button" /> 
     <RadioButton 
      android:id="@+id/radio1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:button="@drawable/radio_button" 
      android:text="Right" 
      android:checked="true" /> 
    </RadioGroup> 

隨着我的選擇

with selector

沒有我的選擇

without selector

我選擇XML

<?xml version="1.0" encoding="UTF-8" ?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android" > 
    <item 
     android:drawable="@drawable/radio_button_checked" 
     android:state_checked="true" /> 
    <item 
     android:drawable="@drawable/radio_button_unchecked" /> 
</selector> 

radio_button_checked.xml

<?xml version="1.0" encoding="UTF-8" ?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="oval"> 
    <solid android:color="@color/primary_button_color"/> 
    <stroke 
     android:width="2px" 
     android:color="#000000"/> 
</shape> 

radio_button_unchecked.xml

<?xml version="1.0" encoding="UTF-8" ?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="oval"> 
    <stroke 
     android:width="2px" 
     android:color="#000000"/> 
</shape> 

回答

2

我想你看不到任何東西,因爲你的繪製沒有任何規模大小都有。

嘗試在xml的形狀下定義<size android:width="60dp" android:height="60dp"/>

+0

修好了,謝謝!現在看起來很明顯! – RobVoisey

1

您的問題

正如你看到的,RadioButton延伸CompoundButton。當你不使用你的選擇器時,有一個默認的drawable。讓我們看看它的構造函數。

public CompoundButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 
    super(context, attrs, defStyleAttr, defStyleRes); 

    final TypedArray a = context.obtainStyledAttributes(
      attrs, com.android.internal.R.styleable.CompoundButton, defStyleAttr, defStyleRes); 

    final Drawable d = a.getDrawable(com.android.internal.R.styleable.CompoundButton_button); 
    if (d != null) { 
     setButtonDrawable(d); 
    } 

默認的繪製是R.styleable.CompoundButton_buttonintrinsicWidth = 96pxintrinsicHeight = 96px我的設備上。

當您使用選擇器時,drawableintrinsicWidth = -1pxintrinsicHeight = -1px因此您看不到它。

解決方案

您應該定義繪圖的大小。

<?xml version="1.0" encoding="UTF-8" ?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="oval"> 
<size android:width="24dp" android:height="24dp"/> 
<stroke 
    android:width="2px" 
    android:color="#000000"/> 
</shape>