我想動態添加按鈕。我加入了多個按鈕動態,但我想添加在下面的模式按鈕:如何動態添加按鈕?
[BUTTON1] [BUTTON2]
[BUTTON3] [BUTTON4]
[BUTTON5] [BUTTON6]
這意味着我要添加一排是隻有動態的2個按鈕。
我已經嘗試了很多選擇。其中 之一是:
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);
注意:按鈕的計數可能是隨機的。
我該怎麼做?
任何以前的嘗試?或只是問? –
請在google上搜索。到目前爲止您嘗試了哪些? –
是的,我已經嘗試了很多..請幫助.. – Android