在TableLayout中动态加载按钮

时间:2013-06-20 06:33:36

标签: android android-button android-tablelayout

我正在尝试在Android代码中动态创建按钮。我有一个字符串数组defnied,我根据它确定要加载多少按钮,并从该字符串数组中选择每个按钮的文本。我在我的活动onCreate()上编写了相同的代码。代码无效。没有错误,但是当我的活动加载时我没有看到按钮。我确实看到屏幕上有一些空间,但按钮不在那里。有人可以在这里发现任何问题来帮助。

以下代码

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>

2 个答案:

答案 0 :(得分: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]);

答案 1 :(得分:0)

我认为你的循环可能有点偏离。

查看原始代码 - &gt;你是为每个按钮添加一行(所以每个按钮都有自己的行)。然后你也可以重置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]);