2012-11-15 52 views
1

我嘗試了下面的代碼通過編程方式創建多個按鈕,但它會根據我的應用程序創建一個按鈕,我需要根據輸入創建一個按鈕。例如,如果輸入是3意味着我需要在佈局中創建三個按鈕。爲了您的參考,我附上了示例圖片和我的代碼。如何在android中創建多個按鈕

for (int i = 0; i < array_of_btn_input.size(); i++) { 
      LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.WRAP_CONTENT, 
        LinearLayout.LayoutParams.WRAP_CONTENT); 

      LinearLayout layout = new LinearLayout(getApplicationContext()); 
      LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.WRAP_CONTENT, 
        LinearLayout.LayoutParams.WRAP_CONTENT); 
      layout.setOrientation(LinearLayout.VERTICAL); 
      layout.setLayoutParams(params); 
      Button button1 = new Button(getApplicationContext()); 
      button1.setLayoutParams(params1); 
      button1.setText("button"); 

      layout.addView(button1); 
      main_layer.addView(layout); 
} 

enter image description here

+1

什麼類型來看主要佈局是什麼?這可能只是將按鈕堆疊在一起。 – BoredAndroidDeveloper

+0

1)爲什麼每個按鈕都需要一個LinearLayout? – fiddler

+0

2)什麼是全球集裝箱? – fiddler

回答

1

如果在您的示例中,您的全局容器(main_layer)是一個相關佈局或框架佈局,您已將它們放在彼此之上。所以你看不到後面的順序。

試試這個PLS,

LinearLayout main_layer= (LinearLayout) findViewById(id.main_layer); 


for (int i = 0; i < 10; i++) 
{ 
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT); 

LinearLayout layout = new LinearLayout(getApplicationContext()); 
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT); 

layout.setOrientation(LinearLayout.VERTICAL); 
layout.setLayoutParams(params); 
Button button1 = new Button(getApplicationContext()); 
button1.setLayoutParams(params1); 
button1.setText("button"); 

layout.addView(button1); 
main_layer.addView(layout); 
} 
0

你內創建多個新的線性佈局循環。線性佈局需要從for循環中取出,並嘗試添加按鈕。

1日的for循環創建了一個新的線性佈局,並增加了一個按鈕運行

第二的for循環創建了一個新的線性佈局,並增加了一個按鈕上以前添加布局

0

的頂部,給你跑創建每按鈕創建一個新的LinearLayout ...嘗試更多的東西一樣......

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
    LinearLayout.LayoutParams.WRAP_CONTENT, 
    LinearLayout.LayoutParams.WRAP_CONTENT); 
LinearLayout layout = new LinearLayout(getApplicationContext()); 
layout.setOrientation(LinearLayout.VERTICAL); 
layout.setLayoutParams(params); 

for (int i = 0; i < array_of_btn_input.size(); i++) { 
    LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(
      LinearLayout.LayoutParams.WRAP_CONTENT, 
      LinearLayout.LayoutParams.WRAP_CONTENT); 

    Button button1 = new Button(getApplicationContext()); 
    button1.setLayoutParams(params1); 
    button1.setText("button"); 

    layout.addView(button1); 

}

main_layer.addView(佈局);

0

試試這個

LinearLayout llParent = (LinearLayout) findViewById(R.id.llParent); 
llParent.removeAllViews(); 
for (int i = 0; i < array_of_btn_input.size(); i++) { 
    BSView b = new BSView(this, new SimpleThumbBean(i + 1, bsList 
      .get(i).getLabel(), bsList.get(i).getThumbUrl())); 
    LinearLayout.LayoutParams llDynamic = new LinearLayout.LayoutParams(
      LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); 
    llDynamic.weight = 1f; 
    llParent.addView(b, llDynamic); 
} 

llParent是確定的佈局,並確保你添加動態佈局到它之前調用removeAllViews()。

BSView是我自己定製的視圖。您可以根據需要將BSView設置爲普通按鈕。