如何动态添加按钮?

时间:2017-02-11 10:05:14

标签: android button layout

我想动态添加按钮。我正在添加多个按钮动态,但我想按以下模式添加按钮:

[BUTTON1] [BUTTON2]
[BUTTON3] [BUTTON4]
[BUTTON5] [BUTTON6]

这意味着我想连续添加两个动态的按钮。

我尝试了很多选择。 其中之一是:

 LinearLayout ll = (LinearLayout) findViewById(R.id.buttonlayout);
    Button[][] buttonArray = new Button[count][count];
    TableLayout table = new TableLayout(this);
    for (int row = 0; row < count; row++) {
        TableRow currentRow = new TableRow(this);
        for (int button = 0; button < row; button++) {
            Button currentButton = new Button(this);
            // you could initialize them here
            currentButton.setOnClickListener(this);
            // you can store them
            buttonArray[row][button] = currentButton;
            // and you have to add them to the TableRow
            currentRow.addView(currentButton);
        }
        // a new row has been constructed -> add to table
        table.addView(currentRow);
    }

最后获取该新表并将其添加到您的布局中。 ll.addView(table);

注意:按钮计数可以是随机的。

我该怎么做?

4 个答案:

答案 0 :(得分:2)

在XML中使用垂直LinearLayout。然后以编程方式创建水平LinearLayout并在水平布局中添加按钮。对于每一行,创建并添加新的水平布局。

XML:

<LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/buttonlayout">
</LinearLayout>

活动:

public class dynamicButtons extends AppCompatActivity {

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

    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT);

    int numberOfRows = 3;
    int numberOfButtonsPerRow = 2;
    int buttonIdNumber = 0;


    final LinearLayout verticalLayout= LinearLayout)findViewById(R.id.buttonlayout);

    for(int i=0;i<numberOfRows;i++){
      LinearLayout newLine = new LinearLayout(this);
      newLine.setLayoutParams(params);
      newLine.setOrientation(LinearLayout.HORIZONTAL);
      for(int j=0;j<numberOfButtonsPerRow;j++){
            Button button=new Button(this);
            // You can set button parameters here:
            button.setWidth(20);
            button.setId(buttonIdNumber);
            button.setLayoutParams(params);
            button.setText("Button Name");
            button.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {

                    Intent is = new Intent(getApplicationContext(), someOtherApplication.class);
                    is.putExtra("buttonVariable", buttonIdNumber);
                    startActivity(is);
                }
            });

            newLine.addView(button);
            buttonIdNumber++;
      }
      verticalLayout.addView(newLine);

    }
   }
  }

答案 1 :(得分:0)

使用自定义项目创建一个listview / recyclerview,如你在问题中提到的那样按住2个按钮,而不是用按钮填充listView(如果项目索引%2 == 0则在适配器内部,否则它将采用左侧位置,否则右侧位置)。

答案 2 :(得分:0)

做这样的事情:

    LinearLayout ll_Main  = new LinearLayout(getActivity());
    LinearLayout ll_Row01 = new LinearLayout(getActivity());
    LinearLayout ll_Row02 = new LinearLayout(getActivity());

    ll_Main.setOrientation(LinearLayout.VERTICAL);
    ll_Row01.setOrientation(LinearLayout.HORIZONTAL);
    ll_Row02.setOrientation(LinearLayout.HORIZONTAL);

    final Button button01    = new Button(getActivity());
    final Button button02    = new Button(getActivity());   
    final Button button03    = new Button(getActivity());
    final Button button04    = new Button(getActivity());

    ll_Row01.addView(button01);
    ll_Row01.addView(button02);

    ll_Row02.addView(button03);
    ll_Row02.addView(button04);

    ll_Main.addView(ll_Row01);
    ll_Main.addView(ll_Row02);

    button04.setVisibility(View.INVISIBLE);
    button04.setVisibility(View.VISIBLE);

答案 3 :(得分:0)

试试这段代码:

TableLayout layout = new TableLayout (this);
    layout.setLayoutParams( new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));

    for (int i=0; i<2; i++) {   //number of rows
        TableRow tr = new TableRow(this);
        for (int j=0; j<2; j++) { //number of columns
            Button b = new Button (this);
            b.setText("Button:"+i+j);
            b.setTextSize(10.0f);
            b.setOnClickListener(this);
            tr.addView(b, 30,30);
        }
        layout.addView(tr);
    }

由于您的按钮数量是随机的,您可以使用:

 int total = 20;  //random number of buttons
int column = 3;    //specify the column number
int row = total / column;  

现在使用columnrow值动态显示按钮

相关问题