我正在动态创建tablerow(9行)。我想平均将表格布局高度划分为表格行。但它不起作用......
我创建9行,每行包含9个TextView
我的xml是
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/header"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="100" >
<TableLayout
android:layout_weight="80"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:background="#000"
android:paddingTop="1dp"
android:paddingLeft="1dp"
android:paddingRight="1dp"
android:paddingBottom="1dp"
android:id="@+id/sudokuboard"
android:stretchColumns="*"
android:weightSum="100" >
</TableLayout>
我的代码是
tblsudoku = (TableLayout) findViewById(R.id.sudokuboard);
//1st Row
for(int i=0; i < 9; i++)
{
TableRow NewRow1 =new TableRow(this);
NewRow1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,TableRow.LayoutParams.FILL_PARENT,10.0f));
NewRow1.setPadding(1, 1, 1, 1);
for(int j=0; j<9; j++)
{
TextView tv_00 = new TextView(this);
int id=10*i;
id=id+j;
String strText=""+id;
tv_00.setText(strText);
tv_00.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,TableRow.LayoutParams.FILL_PARENT, 10.0f));
tv_00.setGravity(Gravity.CENTER);
tv_00.setBackgroundColor(Color.parseColor("#FFFFFF"));
tv_00.setId(id);
NewRow1.addView(tv_00);
}
//tblsudoku.addView(NewRow1, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
tblsudoku.addView(NewRow1);
}
请帮我解决这个问题...
2013年12月29日更新
新代码
tblsudoku = (TableLayout) findViewById(R.id.sudokuboard);
for(int i=0; i < 9; i++)
{
TableRow NewRow1 =new TableRow(this);
NewRow1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,TableRow.LayoutParams.FILL_PARENT,1f));
NewRow1.setBackgroundColor(Color.RED);
LinearLayout Newrowlayout =new LinearLayout(this);
Newrowlayout.setLayoutParams(new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.WRAP_CONTENT,1f));
Newrowlayout.setOrientation(1);
NewRow1.setPadding(1, 1, 1, 1);
for(int j=0; j<9; j++)
{
TextView tv_00 = new TextView(this);
int id=10*i;
id=id+j;
String strText=""+id;
tv_00.setText(strText);
tv_00.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT, 1f));
tv_00.setGravity(Gravity.CENTER);
tv_00.setTextColor(Color.YELLOW);
tv_00.setId(id);
Newrowlayout.addView(tv_00);
}
NewRow1.addView(Newrowlayout);
tblsudoku.addView(NewRow1);
}
设计
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#000"
android:paddingTop="1dp"
android:paddingLeft="1dp"
android:paddingRight="1dp"
android:paddingBottom="1dp"
android:id="@+id/sudokuboard">
</TableLayout>
答案 0 :(得分:2)
关键句是“小部件必须具有其的LayoutParams parent 。“,因此您必须将TableRow.LayoutParams更改为表格行的TableLayout.LayoutParams。 希望它有效!
更多详细信息,请点击Here
答案 1 :(得分:-1)
下次在发布问题之前使用右上角的搜索栏。看看下面的帖子,我相信这就是你要找的东西。