/ / Crear y agregar dinámicamente framelayouts android - java, android, android-layout, framelayout

Cree y agregue dinámicamente framelayouts android - java, android, android-layout, framelayout

He creado un framelayout con un botón y 3 vistas de texto dentro del botón. Quiero duplicar esto y agregarlos dinámicamente a través del programa java uno debajo del otro.

Se adjunta el código xml para el mismo.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout android:id="@+id/frameLayout1" android:layout_width="wrap_content" android:layout_height="wrap_content">
<Button android:layout_width="319dp" android:layout_height="130dp"/>
<TextView android:id="@+id/textView1" android:layout_width="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="15dp"
android:layout_height="wrap_content" android:text="@string/app_name"/>
<TextView android:id="@+id/textView2"
android:layout_gravity="center_horizontal"
android:layout_marginTop="45dp"
android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/app_name"/>
<TextView android:id="@+id/textView3"
android:layout_gravity="center_horizontal"
android:layout_marginTop="75dp"
android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/app_name"/>
</FrameLayout>
<FrameLayout android:id="@+id/frameLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/frameLayout1">
<Button android:layout_width="319dp" android:layout_height="130dp"/>
<TextView android:id="@+id/textView4" android:layout_width="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="15dp"
android:layout_height="wrap_content" android:text="@string/app_name"/>
<TextView android:id="@+id/textView5"
android:layout_gravity="center_horizontal"
android:layout_marginTop="45dp"
android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/app_name"/>
<TextView android:id="@+id/textView6"
android:layout_gravity="center_horizontal"
android:layout_marginTop="75dp"
android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/app_name"/>
</FrameLayout>
</RelativeLayout>

El código java para esta duplicación y anexión al final viene dado por,

FrameLayout f2 = (FrameLayout)findViewById(R.id.frameLayout2);
FrameLayout f3;
f3 = f2;
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
p.addRule(RelativeLayout.BELOW, R.id.frameLayout2);
f3.setLayoutParams(p);

Quiero que el diseño del tercer cuadro se coloque debajo del segundo, pero al ejecutar este código, el segundo diseño del marco tampoco es visible. ¿Alguien podría decirme dónde me voy mal?

Respuestas

1 para la respuesta № 1

Debe extraer el diseño del marco en un archivo button_and_text.xml separado y luego usar el incluir etiqueta.

<RelativeLayout ...>
<include layout="@layout/button_and_text" android:id="@+id/frameLayout1"/>
<include layout="@layout/button_and_text" android:id="@+id/frameLayout2" android:layout_below="@id/frameLayout1"/>