/ / Creare e aggiungere dinamicamente framelayouts android - java, android, layout-android, framelayout

Crea dinamicamente e aggiungi framelayouts android - java, android, layout-android, framelayout

Ho creato un framelayout con un pulsante e 3 visualizzazioni di testo all'interno del pulsante. Voglio duplicare questo e aggiungerli dinamicamente attraverso il programma java uno sotto l'altro.

Il codice xml per lo stesso è allegato.

<?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>

Il codice java per questa duplicazione e l'aggiunta alla fine è dato da,

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);

Voglio che il terzo frame sia posizionato sotto il secondo, ma eseguendo questo codice, anche il secondo layout del frame non è visibile, qualcuno potrebbe dirmi dove sto andando male?

risposte:

1 per risposta № 1

È necessario estrarre il layout della cornice in un file button_and_text.xml separato e quindi utilizzare il file include tag.

<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"/>