RadioGroup extension RelativeLayout?
-
27-10-2019 - |
Question
Je suis en train de faire une grille de boutons radio pour mon application, ce que j'ai appris que ce n'est pas possible à l'aide RadioGroup
régulière, car elle étend LinearLayout et si vous essayez d'organiser la RadioButtons
en utilisant RelativeLayout INSIDE le RadioGroup
la RadioGroup
ne voit pas l'intérieur du Buttons
RelativeLayout
.
Ainsi, afin de résoudre ce problème, je veux faire un RadioGroup personnalisé qui étend RelativeLayout au lieu de LinearLayout.
Comment puis-je faire?
Mise à jour: Je ne que vous avez dit, mais j'ai ces erreurs, je ne sais pas comment résoudre dans le fichier de classe:
Description Resource Path Location Type
RadioGroup_checkedButton cannot be resolved or is not a field RadioGroupRelative.java /BlockBall/src/com/stickfigs/blockball line 81 Java Problem
The constructor RelativeLayout.LayoutParams(int, int, float) is undefined RadioGroupRelative.java /BlockBall/src/com/stickfigs/blockball line 265 Java Problem
The method setOnCheckedChangeWidgetListener(CompoundButton.OnCheckedChangeListener) is undefined for the type RadioButton RadioGroupRelative.java /BlockBall/src/com/stickfigs/blockball line 363 Java Problem
The method setOnCheckedChangeWidgetListener(null) is undefined for the type RadioButton RadioGroupRelative.java /BlockBall/src/com/stickfigs/blockball line 377 Java Problem
VERTICAL cannot be resolved to a variable RadioGroupRelative.java /BlockBall/src/com/stickfigs/blockball line 68 Java Problem
Widget_CompountButton_RadioButton cannot be resolved or is not a field RadioGroupRelative.java /BlockBall/src/com/stickfigs/blockball line 79 Java Problem
La solution
Vous devez obtenir le code source du RadioGroup
ici , remplacer toutes les entrées de LinearLayout
avec RelativeLayout
Ajoutez ce code à un certain fichier xml dans votre projet (généralement son nom est attrs.xml):
<resources>
<declare-styleable name="RadioGroup">
<attr name="android:checkedButton" />
</declare-styleable>
</resources>
Remplacer les constructeurs de RadioGroup
avec celles-ci:
public RadioGroup(Context context) {
super(context);
if (!isInEditMode()) {
init();
}
}
public RadioGroup(Context context, AttributeSet attrs) {
super(context, attrs);
if (!isInEditMode()) {
TypedArray attributes = context.obtainStyledAttributes(
attrs, R.styleable.RadioGroup, 0,
android.R.style.Widget_CompoundButton_RadioButton);
int value = attributes.getResourceId(R.styleable.RadioGroup_checkedButton,
View.NO_ID);
if (value != View.NO_ID) {
mCheckedId = value;
}
attributes.recycle();
init();
}
}
Retirez le constructeur suivant de la classe LayoutParams
intérieure:
public LayoutParams(int w, int h, float initWeight) {
super(w, h, initWeight);
}
remplacer toutes les occurrences d'appels de méthode setOnCheckedChangeWidgetListener()
avec la méthode setOnCheckedChangeListener()
. IMPORTANT :. Dans ce cas, il ne sera pas possible d'utiliser cette méthode à partir d'un code qui utilise ce widget
ai pas essayé, mais espérons que cela fonctionnera.
Autres conseils
Copie la source RadioGroup ici et de le modifier pour changer d'étendre RelativeLayout au lieu de LinearLayout.