2013-06-20 113 views
0

我想在我的Android代碼中動態地創建按鈕。我有一個字符串數組defnied根據我找出多少個按鈕加載和每個按鈕的文本從字符串數組中挑選。我在下面的活動onCreate()上寫下了相同的代碼。代碼不起作用。沒有錯誤,但我的活動加載時沒有看到按鈕。我確實看到屏幕上有一些空間被佔用,但按鈕不在那裏。有人可以找到任何問題來幫助。在TableLayout中動態加載按鈕

代碼如下

TableLayout table = (TableLayout) findViewById(R.id.tableLayoutCategoryButtons); 
int buttonsInRow = 3;//number of buttons in each row 
String[] itemNames = getResources().getStringArray(R.array.categories_array); 
int numRows = (itemNames.length/buttonsInRow); 
//round off numRows to next integer. e.g. for 10 items in array there will be (10/3) +1=4 rows 
if (itemNames.length % buttonsInRow != 0) 
    numRows++; 
TableRow[] tr = new TableRow[numRows]; 
Button[] buttons = new Button[itemNames.length]; 
int rowcount = 0; 
for (int i = 0; i < itemNames.length; i++) { 
    buttons[i] = new Button(table.getContext(), null, android.R.attr.buttonStyleSmall); 
    buttons[i].setText(itemNames[i]); 
    buttons[i].setTextColor(Color.BLACK); 
    buttons[i].setVisibility(View.VISIBLE); 
    buttons[i].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
    tr[rowcount] = new TableRow(table.getContext()); 
    tr[rowcount].setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); 
    tr[rowcount].addView(buttons[i]); 

    //change of tablerow, once a multiple of 3 reached. 
    if (((i + 1) % buttonsInRow == 0) && (i != 0)) { 
     tr[rowcount].setVisibility(View.VISIBLE); 
     table.addView(tr[rowcount]); 

     rowcount++; 
    } 
} 

正如你可以看到我創建一個TableRows每三個按鈕在一排。然後將Tablerow添加到TableLayout視圖。而我的活動xml低於

<RelativeLayout 
    android:id="@+id/relativeLayoutCategoryHeader" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 
    <TableLayout 
     android:id="@+id/tableLayoutCategoryButtons" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:stretchColumns="0,1,2"> 
    </TableLayout> 
</RelativeLayout> 

回答

1

我做了一些改變。希望對你有效。

 LinearLayout[] tablerowLayout = new LinearLayout[numRows]; 
     tablerowLayout[0] = new LinearLayout(table.getContext()); 
     int rowcount=0; 
     for (int i=0; i<itemNames.length; i++){ 
      buttons[i]= new Button(table.getContext(), null, android.R.attr.buttonStyleSmall); 
      buttons[i].setText(itemNames[i]); 
      buttons[i].setTextColor(Color.BLACK); 
      buttons[i].setVisibility(View.VISIBLE); 

      LinearLayout.LayoutParams buttonLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); 
      tablerowLayout[rowcount].addView(buttons[i], buttonLayoutParams); 

      if (((i+1)%buttonsInRow==0)&&(i!=0)){ 
       table.addView(tablerowLayout[rowcount++]); 
       if(rowcount<numRows) 
        tablerowLayout[rowcount] = new LinearLayout(table.getContext()); 
      } 
     } 
     if(rowcount<numRows) 
      table.addView(tablerowLayout[rowcount]); 
+0

謝謝。現在IT工作。你是對的,爲每一行添加一個線性佈局。 – user1938357

0

我覺得你的循環可能有點偏離這裏。看看你的原始代碼 - >你爲每個按鈕添加一行(所以每個按鈕都有它自己的行)。然後,您還可以重置tr [rowcount],即tr [0]設置約4次,每次只添加1個按鈕。

下面我做了一個快速修復,它可能不起作用,但它顯示瞭如何在必要時只想添加一行(而不是重寫它的加時)。 (趕上巴士)

rowCount = 0; 
int lastRowAdded = 0; 
for (int i=0; i<itemNames.length; i++){ 

    if (i % buttonsInRow == 0){ 
     tr[rowcount] = new TableRow(table.getContext()); 
     tr[rowcount].setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT)); 

     tr[rowcount].setVisibility(View.VISIBLE); 

     table.addView(tr[rowcount]); 
     lastRowAdded = rowCount; 
     rowcount++; 
    } 

    buttons[i]= new Button(table.getContext(),null,android.R.attr.buttonStyleSmall); 
    buttons[i].setText(itemNames[i]); 
    buttons[i].setTextColor(Color.BLACK); 
    buttons[i].setVisibility(View.VISIBLE); 
    buttons[i].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); 
    tr[lastRowAdded].addView(buttons[i]); 
+0

我在每一行都加了三個按鈕。 int buttonsInRow = 3; 捕捉到了這一點。然後我在numRows中獲得總行數的值。然後我將numRows舍入到下一個整數。所以如果我有10個項目。我會有4行,前3個每個3個按鈕,最後一行有一個按鈕。 – user1938357