2017-10-12 3 views
1

J'ai essayé de changer le drawable du bouton coché sur un RadioButton mais rien ne montre; la définition du drawable supprime simplement celle par défaut qui est déjà là.RadioButton 'android: bouton' ne fait rien

J'ai utilisé le débordement de pile pour essayer de trouver une solution, mais quand j'essaie de l'implémenter, il n'y a pas d'affichage apparaissant. Mon sélecteur semble être bien, car lorsque je définis le background du bouton radio qui affiche mes cercles.

Je suis sûr que c'est un problème très évident que je vais me gifler de ne pas voir; quelqu'un peut-il comprendre ce qui ne va pas?

Ma radio Boutons

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

Avec mon sélecteur

with selector

Sans mon sélecteur

without selector

Mon 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> 

Répondre

2

Je pense que vous ne pouvez pas voir quoi que ce soit parce que votre drawable ne pas de taille.

Essayez de définir <size android:width="60dp" android:height="60dp"/> sous la forme de votre fichier XML.

+0

Correction, merci! Cela semble évident maintenant! – RobVoisey

1

Votre problème

Comme vous le voyez, RadioButton étend CompoundButton. Lorsque vous n'utilisez pas votre sélecteur, il y a un drawable par défaut. Voyons son constructeur.

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); 
    } 

Le drawable par défaut est R.styleable.CompoundButton_button dont intrinsicWidth = 96px et intrinsicHeight = 96px sur mon appareil. Lorsque vous utilisez votre sélecteur, le drawable affiche intrinsicWidth = -1px et intrinsicHeight = -1px. Vous ne pouvez donc pas le voir.

Solution

Vous devez définir la taille de votre drawable.

<?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>