2017-02-11 101 views
-1

我想動態添加按鈕。我加入了多個按鈕動態,但我想添加在下面的模式按鈕:如何動態添加按鈕?

[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);

注意:按鈕的計數可能是隨機的。

我該怎麼做?

+0

任何以前的嘗試?或只是問? –

+2

請在google上搜索。到目前爲止您嘗試了哪些? –

+0

是的,我已經嘗試了很多..請幫助.. – Android

回答

0

創建一個listview/recyclerview與自定義項目持有2按鈕,如你在你的問題中提到,比使用按鈕填充該listView(適配器內,如果項目索引%2 == 0它將採取左側的位置,否則正確的位置)。

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); 
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); 
    } 

至於你的按鈕的數量是隨機的u可以使用:

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

現在使用的columnrow值動態顯示按鈕

+0

它會添加4個按鈕..我說的按鈕的計數可能是隨機的.. – Android

+0

檢查編輯.. – rafsanahmad007

1

在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); 

    } 
    } 
    }