2016-11-24 4 views
-1

Ich habe ein Layout mit 4 Floating-Etiketten und darunter eine Schaltfläche.Android: Hinzufügen und Entfernen von Layouts programmgesteuert

Ich möchte dem Benutzer eine Möglichkeit geben, dass, wenn sie auf die Taste unten, damit er ein neues Layout zu zeigen, dass Textview und Imageview auf jedem Klick (bis zu 3 Layouts)

See example of requested Layout

Meine Frage einschließlich Was ist das beste Design für ein solches Layout?

Fügen Sie Ald-Layouts mit Visibily GONE hinzu und klicken Sie auf die Schaltfläche, um jedes Mal ein Layout anzuzeigen?

Oder fügen Sie sie programmgesteuert hinzu?

Auch ich möchte dem Benutzer eine Option geben, um jedes gewünschte Layout von den insgesamt drei zu löschen.

Vielen Dank im Voraus.

Antwort

0

Die einfachste Option ist wahrscheinlich genau das, was Sie vorgeschlagen haben - Hinzufügen aller Layouts in der XML-Layoutdatei und Einstellen der Sichtbarkeit der einzelnen Layouts auf "GONE". Wenn Sie viele weitere Layouts hinzufügen möchten, können Sie sie programmatisch erstellen, um Ressourcen zu sparen und nicht alle Bilder zu laden, wenn die Aktivität erstellt wird.

Sie werden wahrscheinlich das Layout mit allem darin einfügen (sieht aus wie ein vertikales lineares Layout für mich) in eine ScrollView, so dass die Layouts nicht aus dem Rahmen bewegen, wenn sichtbar gesetzt.

Sie benötigen also eine onClick-Funktion für den Button, die das nächste Layout sichtbar macht, das nicht sichtbar ist, und ein anderes, das das Layout wieder verbirgt.

Ihre xml könnte wie folgt aussehen (werfen diese in jedes Layout, das Sie verwenden möchten, habe ich eine vertikale Linear Layout):

<Button 
    android:text="Button" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/button" 
    android:onClick="addLayout" /> 

<LinearLayout 
    android:orientation="vertical" 
    android:layout_weight="1" 
    android:visibility="gone" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/layout1"> 

    <TextView 
     android:text="TextView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/textView" 
     android:layout_gravity="center_horizontal" /> 

    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     app:srcCompat="@mipmap/ic_launcher" 
     android:id="@+id/imageView" /> 

    <Button 
     android:text="hide" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/button2" 
     android:onClick="hideParent (MainActivity)" /> 
</LinearLayout> 

<LinearLayout 
    android:orientation="vertical" 
    android:layout_weight="1" 
    android:visibility="gone" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/layout2"> 

    <TextView 
     android:text="TextView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/textView" 
     android:layout_gravity="center_horizontal" /> 

    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     app:srcCompat="@mipmap/ic_launcher" 
     android:id="@+id/imageView" /> 

    <Button 
     android:text="hide" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/button3" 
     android:onClick="hideParent (MainActivity)" /> 
</LinearLayout> 

<LinearLayout 
    android:orientation="vertical" 
    android:layout_weight="1" 
    android:visibility="gone" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/layout3"> 

    <TextView 
     android:text="TextView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/textView" 
     android:layout_gravity="center_horizontal" /> 

    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     app:srcCompat="@mipmap/ic_launcher" 
     android:id="@+id/imageView" /> 

    <Button 
     android:text="hide" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/button4" 
     android:onClick="hideParent (MainActivity)" /> 

</LinearLayout> 

Wie ich schon sagte, könnte es hilfreich sein, eine ScrollLayout umschlingen alle Das ist nur die Grundidee. Ihr Haupt könnte wie folgt aussehen:

LinearLayout[] layouts; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    layouts = new LinearLayout[3]; 
    layouts[0] = (LinearLayout) findViewById(R.id.layout1); 
    layouts[1] = (LinearLayout) findViewById(R.id.layout2); 
    layouts[2] = (LinearLayout) findViewById(R.id.layout3); 
} 

public void hideParent(View v) { 
    ((LinearLayout)v.getParent()).setVisibility(View.GONE); 
} 

public void addLayout(View v) { 
    for (int i = 0; i <= 2; i++) { 
     if (layouts[i].getVisibility() == View.GONE) { 
      layouts[i].setVisibility(View.VISIBLE); 
      break; 
     } 
    } 
} 

Hoffnung, dass Ihre tatsächliche Art und Weise der Codierung hilft, hängt sehr stark von der Flexibilität und der Anzahl von Layouts Sie erreichen wollen.

+0

Danke Mann! Genau das wollte ich genau. Nur eine Korrektur: Die for-Schleife sollte 'für (int i = 0; i <3; i ++)' –

+0

Ich bin froh, dass ich helfen konnte. Yup, du hast Recht, danke, habe es gerade bearbeitet. –

Verwandte Themen