动态加载按钮TableLayout [英] loading buttons dynamically in TableLayout
问题描述
我想在我的Android code动态创建按钮。我有一个字符串数组defnied在此基础上我想出多少按钮加载并为每个按钮上的文字是从字符串数组采摘。我已经写了code低于同我的活动的onCreate()
。在code不工作。有没有错误,但我看不到按钮时,我的活动负载。我看到正在采取在屏幕上的一些空间,但是按键不存在。可有人发现任何问题,在这里帮助。
code以下
TableLayout表=(TableLayout)findViewById(R.id.tableLayoutCategoryButtons);
INT buttonsInRow = 3;在每行中的按钮//数
的String [] = itemNames的getResources()getStringArray(R.array.categories_array)。
INT其行=(itemNames.length / buttonsInRow);
//其行四舍五入到下一个整数。例如对10个项目阵列会有(10/3)+ 1 = 4行
如果(itemNames.length%buttonsInRow!= 0)
其行++;
的TableRow [] = TR新的TableRow [其行]
按钮[]按钮=新的Button [itemNames.length]
INT行数= 0;
的for(int i = 0; I< itemNames.length;我++){
按钮[i] =新按钮(table.getContext(),空,android.R.attr.buttonStyleSmall);
按钮[I] .setText(itemNames的[I]);
按钮[I] .setTextColor(Color.BLACK);
按钮[I] .setVisibility(View.VISIBLE);
按钮[I] .setLayoutParams(新的LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
TR [行数] =新的TableRow(table.getContext());
TR [行数] .setLayoutParams(新的LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
TR [行数] .addView(按钮[I]); // tablerow的的变化,一旦为3的倍数到达。
如果((第(i + 1)%buttonsInRow == 0)及&放大器;!(I = 0)){
TR [行数] .setVisibility(View.VISIBLE);
table.addView(TR [行数]); ROWCOUNT ++;
}
}
正如你可以看到我创建一个TableRows每三个按钮在一排。然后加入tablerow的到TableLayout图。而我的活动下的xml
< RelativeLayout的
机器人:ID =@ + ID / relativeLayoutCategoryHeader
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:方向=横向>
< TableLayout
机器人:ID =@ + ID / tableLayoutCategoryButtons
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:stretchColumns =0,1,2>
< / TableLayout>
< / RelativeLayout的>
我做了一些改动。希望它为你工作。
的LinearLayout [] = tablerowLayout新的LinearLayout [其行]
tablerowLayout [0] =新的LinearLayout(table.getContext());
INT行数= 0;
的for(int i = 0; I< itemNames.length;我++){
按钮[i] =新按钮(table.getContext(),空,android.R.attr.buttonStyleSmall);
按钮[I] .setText(itemNames的[I]);
按钮[I] .setTextColor(Color.BLACK);
按钮[I] .setVisibility(View.VISIBLE); LinearLayout.LayoutParams buttonLayoutParams =新LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
tablerowLayout [行数] .addView(按钮[I],buttonLayoutParams); 如果((第(i + 1)%buttonsInRow == 0)及&放大器;!(I = 0)){
table.addView(tablerowLayout [行数++]);
如果(行数<其行)
tablerowLayout [行数] =新的LinearLayout(table.getContext());
}
}
如果(行数<其行)
table.addView(tablerowLayout [行数]);
I am trying to create buttons dynamically in my Android code. 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()
. 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. And then adding the tablerow to TableLayout view. And my activity xml below
<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]);
这篇关于动态加载按钮TableLayout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!