Я хочу создать такую страницу. эти 7 кнопок уже существуют, но если пользователь хочет добавить больше категорий (кнопка), то он может использовать кнопку + и удалить с помощью кнопки -. Любая идея или учебник для этого?
Я хочу создать такую страницу. эти 7 кнопок уже существуют, но если пользователь хочет добавить больше категорий (кнопка), то он может использовать кнопку + и удалить с помощью кнопки -. Любая идея или учебник для этого?
Создать/удалить кнопку onClick
из + button
и - button
, как показано ниже:
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;
}
}
это для динамического создания кнопки в android
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);
чтобы удалить то же самое, просто измените этот "mainLayout.addView(addButton)
" на removeView или setVisibility кнопки на View.GONE
Это довольно просто.
Button button1=new Button(context);
button1.setText("test");
button1.setId(id);
containerlayout.add(button1);
Надеюсь, это поможет вам.
Если вы хотите создать динамическое представление (например, EditText, textview и т.д.), просто используйте этот код и запустите его в своем приложении.
MyActivity.java://your java файл
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);
В XML файле:
<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" >