Comment ajouter dynamiquement des radiobuttons et ne pas perdre le contenu de la vue
-
12-11-2019 - |
Question
Ma mise en page ressemble à ça
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="Please select the restaurant you want to rate: " />
<RadioGroup android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/SelectRestaurant">
</RadioGroup>
<Button android:layout_width="100px"
android:layout_height="wrap_content"
android:text="Rate it"
android:id="@+id/submitSelection"/>
</LinearLayout>
Lorsque j'ajoute des radiobuttons au radiogroup dans le fichier java, les radiobuttons se chevauchent et le bouton Soumettre a disparu. Je suppose que les radiobuttons remplacent d'autres éléments de vue. Comment puis-je ajouter dynamiquement ces boutons radio sans perdre d'autres éléments.
Le code Java ressemble à ceci:
setContentView(R.layout.submit_select);
RadioGroup radioGroup = (RadioGroup)findViewById(R.id.SelectRestaurant);
for (String restaurant : restaurants) {
RadioButton radioButton = new RadioButton(getBaseContext());
radioButton.setText(restaurant);
radioGroup.addView(radioButton);
}
radioGroup.invalidate();
dismissDialog(DIALOG_SUBMIT_ID);
rateItButton = (Button) findViewById(R.id.submitSelection);
rateItButton.setOnClickListener(this);
La solution
Essayez de créer des paramètres de mise en page avec wrap_content pour la largeur et la hauteur, puis ajouter les LayoutParams lorsque vous ajoutez le bouton au groupe:
radiogroup.addView(newRadioButton, layoutParams);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow