Ich möchte eine solche Seite erstellen. Diese 7 Schaltflächen sind bereits vorhanden, aber wenn der Benutzer weitere Kategorien (Schaltflächen) hinzufügen möchte, kann er die Tasten + verwenden und die Tasten - / löschen. Eine Idee oder ein Tutorial für das Erstellen?
Erstellen/Entfernen-Button onClick
von + button
und - button
wie folgt:
public void onClick(View v) {
switch(v.getId()){
case (R.id.plusbutton):
Button myButton = new Button(this);
myButton.setText("Add Me");
LinearLayout ll = (LinearLayout)findViewById(R.id.buttonlayout);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
ll.addView(myButton, lp);
break;.
case (R.id.minusbutton):
Button myButton = new Button(this);
myButton.setText("Remove Me");
LinearLayout ll = (LinearLayout)findViewById(R.id.buttonlayout);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
ll.removeView(myButton, lp);
break;
}
}
dies ist für die Schaltfläche Erstellen in Android dynamisch
LinearLayout row2 = (LinearLayout) findViewById(R.id.hll2);
Button ivBowl = new Button(this);
ivBowl.setText("hi");
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(70, 70);
layoutParams.setMargins(5, 3, 0, 0); // left, top, right, bottom
ivBowl.setLayoutParams(layoutParams);
row2.addView(ivBowl);
LinearLayout mainLayout = (LinearLayout)findViewById(R.id.yourlayoutidthatisonethepicture);
Button addButton =new Button(this);
addButton.setText("add");
mainLayout.addView(addButton);
zu entfernen ist das gleiche, ändern Sie einfach "mainLayout.addView(addButton)
", um "viewView" oder "viewVisibility of button" auf View.GONE zu setzen
Es ist ziemlich einfach.
Button button1=new Button(context);
button1.setText("test");
button1.setId(id);
containerlayout.add(button1);
Hoffe das hilft dir.
Wenn Sie eine dynamische Ansicht erstellen möchten (wie EditText, Textview usw.), verwenden Sie einfach diesen Code und führen Sie ihn in Ihrer Anwendung aus.
MyActivity.Java://Ihre Java-Datei
LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayout1);
EditText et = new EditText(v.getContext());
et.setText("My new Edit Text);
et.setMinLines(1);
et.setMaxLines(3);
ll.addView(et);
In XML-Datei:
<LinearLayout
Android:id="@+id/linearLayout1"
Android:layout_width="match_parent"
Android:layout_height="match_parent"
Android:layout_alignBottom="@+id/TextView01"
Android:layout_below="@+id/relativeLayout1"
Android:orientation="vertical" >