0

Ich habe ein Layout erstellt, die dieses LayoutInflating Nested-Layout mit RadioButton- zu einem Container

Problem Radiobutton enthalten zu einem Behälter aufgeblasen werden: Das Layout aufgeblasen, aber alle Optionsschaltfläche angezeigt bekommt, ist das falsch? .

Layout enthält Optionsfeld zum Aufblasen im Container.

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="23dp" 
    android:layout_marginTop="10dp" 
    android:orientation="horizontal"> 
    <RadioButton 
    android:id="@+id/radioButton" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"/> 
</LinearLayout> 

-Layout für den Container

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <RadioGroup 
    android:id="@+id/container" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"/> 
</LinearLayout> 

Code, um das Kind Inflating

LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    for (int i = 0 ; i < 6 ; i++){ 
     LinearLayout child =(LinearLayout) inflater.inflate(R.layout.layout_linear_with_rb,container,false); 
     container.addView(child); 
    } 

Screenshot:RadioButton screenshot containing someview

Antwort

0

Das Problem hier ändert, ist die Radiogroup es nur für ein RadioButton- Kind sieht, und weil Im Bei Verwendung eines verschachtelten Layouts, das den RadioButton enthält, kann die RadioGroup den RadioButton nicht finden, der programmatisch aufgeblasen wird.

Wie ich dieses Problem lösen kann ist mit der https://github.com/worker8/RadioGroupPlus das ist ein Tweak RadioGroup, die tief auf verschachtelten Layout graben und finden Sie den RadioButton im Inneren.

0

Radiogroup documentation

Die Auswahl wird durch die eindeutige ID des Optionsfelds identifiziert, wie in der XML-Layout-Datei definiert.

Und mit Blick auf Ihre Codebasis, sehe ich, dass Sie keine eindeutige ID für Ihren RadioButton haben.

Ich machte ein Beispielprojekt und versuchte RadioButton's mit einzigartigen Ids dynamisch hinzuzufügen und es funktioniert tadellos.

RadioGroup container = findViewById(R.id.container); 

    for (int i = 0 ; i < 6 ; i++){ 
     RadioButton radioButton = new RadioButton(this); 
     radioButton.setId(i); 
     container.addView(radioButton); 
    } 

Möglicherweise besteht in diesem Fall ein Problem mit widersprüchlichen IDs. Vielleicht wird eine ID von 0 auf eine andere Ansicht gesetzt. Um solche Verwirrung zu vermeiden, empfehle ich, View.generateViewId() zu verwenden, um eine eindeutige ID zu generieren.

View.generateViewId() ist von API nur> = 17.

bearbeiten 1

Bitte in Ihrem RadioButton- Layout mit Linearlayout als Eltern stoppen. Eine schnelle Lösung für Sie die RadioButton- Layout-Datei

<RadioButton xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/radioButton" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 

Und wenn Sie Ihren Java-Code

LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
for (int i = 0 ; i < 6 ; i++){ 
    RadioButton radioButton = (RadioButton) inflater.inflate(R.layout.layout_linear_with_rb,container,false); 
    radioButton.setId(i); 
    container.addView(radioButton); 
} 
+0

Ich habe ein Problem, das LinearLayout, dass das Elternteil des RadioButton enthält einige Ansicht, ich habe es nicht in meine Frage aufgenommen Es tut mir leid. – tabztabarangao

+0

Versuchen Sie, auch die LinearLayouts mit eindeutigen IDs zu versehen. –

+0

Ich habe den Screenshot meines Radiobutton-Layouts hinzugefügt – tabztabarangao

Verwandte Themen