简体   繁体   English

在TableLayout中动态加载按钮

[英]loading buttons dynamically in TableLayout

I am trying to create buttons dynamically in my Android code. 我试图在我的Android代码中动态创建按钮。 I have a string array defnied based on which I figure out how many buttons to load and the text for every button is picked from that string array. 我定义了一个字符串数组,据此可以确定要加载多少个按钮,并从该字符串数组中选取每个按钮的文本。 I have written the code below for the same on my Activity onCreate() . 我已经在Activity onCreate()上为其编写了以下代码。 The code is not working. 该代码不起作用。 There are no errors but I don't see the buttons when my activity loads. 没有错误,但是在我的活动加载时我看不到按钮。 I do see some space being taken up on the screen but buttons are not there. 我确实看到屏幕上占用了一些空间,但是按钮不在那里。 Can someone spot any issues here to help. 有人可以在这里发现任何问题来提供帮助。

Code below 下面的代码

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

As you can see I am creating a TableRows with three buttons each in a row. 如您所见,我正在创建一个TableRows,每行三个按钮。 And then adding the tablerow to TableLayout view. 然后将tablerow添加到TableLayout视图。 And my activity xml below 还有我的活动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>

I made a few changes. 我做了一些更改。 Hope it works for you. 希望对你有效。

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

I think your loop may be a bit off here. 我认为您的循环可能在这里有些偏离。

Looking at your original code -> you are adding a row for every button (so each button has it's own row). 查看原始代码->您将为每个按钮添加一行(因此每个按钮都有自己的行)。 You also then reset the tr[rowcount] ie tr[0] gets set about 4 times, each time only having 1 button added to it. 然后,您还重置tr [rowcount],即tr [0]被设置大约4次,每次仅添加1个按钮。

Below I did a quick fix, it might not work, but it sort of shows how you would only want to add a row when necessary (and not rewrite it evertime). 在下面,我做了一个快速修复,它可能无法正常工作,但是它显示了您如何只在需要时才添加一行(而不是每次都重写它)。 (Gotta catch a bus) (一定要坐公共汽车)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM