Ajouter plusieurs vues personnalisées à disposition par programmation

Si j'ai, par exemple, sont vides de mise en page comme ceci:

layout1.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

</LinearLayout>

Et quelques autres mises en page comme ceci:

layout2.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView 1" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 2" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 3" 
        android:layout_weight="1"/>

</LinearLayout>

Je voudrais ajouter layout2.xml à layout1.xml par programmation. J'aurais besoin d'avoir plusieurs version différente layout2.xml qui j'ajouterais quand je veux. Chacun aurait un texte différent sur TextView et des Boutons.

En cas d'Android Vue j'avais l'habitude de le faire de la même manière comme cela avec addView:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    TextView tv1 = new TextView(this);
    tv1.setText("HELLO");
    my_linear_layout.addView(tv1);

    Button btn2 = new Button(this);
    bt2.setText("WORLD");
    my_linear_layout.addView(btn2);
}

Comment puis-je faire si je n'ai pas quelque chose de simple comme TextView ou d'un Bouton, et si j'ai mon propre xml défini?

Serait-il possible de faire en quelque sorte de mettre tous les points de vue à l'intérieur de l'adaptateur comme pour une liste et obtenir ensuite lorsque nécessaire et il suffit d'appeler addView sur ces points de Vue?

Utilisation LayoutInflator pour ce faire, Reportez-vous à stackoverflow.com/questions/2335813/...

OriginalL'auteur user3304086 | 2014-11-25