Android 表格布局表格行布局对齐
我完全不明白为什么test、test1、test2、test3不能正确地与第一行对齐。第一行是指测试,2012,测试,测试。我希望下一行跟在表中的第一行后面。请帮忙。提前谢谢 xmlAndroid 表格布局表格行布局对齐,android,tablelayout,tablerow,Android,Tablelayout,Tablerow,我完全不明白为什么test、test1、test2、test3不能正确地与第一行对齐。第一行是指测试,2012,测试,测试。我希望下一行跟在表中的第一行后面。请帮忙。提前谢谢 xml 代码: TableLayout; tl=(TableLayout)findviewbyd(R.id.tl); 对于(int x=0;x
代码:
TableLayout;
tl=(TableLayout)findviewbyd(R.id.tl);
对于(int x=0;x<3;x++){
//创建表行
TableRow tr=新的TableRow(本);
tr.setId(100+x);
TextView lblrecID=新的TextView(此);
lblrecID.setId(200+x);
lblrecID.setText(“测试”);
lblrecID设置重力(重心);
LayoutParams-params=new TableRow.LayoutParams(0,LayoutParams.WRAP_内容,1f);
lblrecID.setLayoutParams(参数);
tr.addView(lblrecID);//在此处将列添加到表行
TextView lblrecName=新的TextView(此);
lblrecName.setId(200+x);//定义必须唯一的id
lblrecName.setText(“test1”);
lblrecName.setGravity(重心);
lblrecName.setLayoutParams(参数);
tr.addView(lblrecName);//在此处将列添加到表行
TableLayout tl1=新的TableLayout(本);
LayoutParams params1=新的TableRow.LayoutParams(0,LayoutParams.WRAP_内容,3f);
tl1.SetStretCharl列(真);
tl1.setLayoutParams(params1);
TableRow tr2=新的TableRow(本);
TextView lblrecID1=新的TextView(此);
lblrecID1.setId(200+x);
lblrecID1.setText(“test2”);
lblrecID1.设置重力(重心);
tr2.addView(lblrecID1);//在此处将列添加到表行
TextView lblrecName1=新的TextView(此);
lblrecName1.setId(200+x);//定义必须唯一的id
lblrecName1.setText(“test3”);
lblrecName1.setGravity(重心);
tr2.addView(lblrecName1);//在此处将列添加到表行中
tl1.addView(tr2);
//最后将其添加到表行
tr.addView(tl1);
//tl.addView(tr,newtablelayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
tl.addView(tr,newtablelayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
}
结果
好的。我没有得到你想要的。可能是这样的布局吗
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tl"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="*">
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#27160e" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="Project"
android:textColor="#fad06e"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="Project2"
android:textColor="#fad06e"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Cost"
android:textColor="#fad06e"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="2011"
android:textColor="#fad06e"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="2012"
android:textColor="#fad06e"
android:textSize="18sp"
android:textStyle="bold" />
</TableRow>
<TableRow>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="test" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="test" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="test" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="test" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="test" />
</TableRow>
<TableRow>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="test" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="test" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="test" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="test" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="test" />
</TableRow>
编辑了我的答案。请说得更具体些。你想要什么?
成本
列应该为空,或者什么?对齐不正确。测试是否可以与测试列对齐,测试1是否可以与2012列对齐,等等。。。
TableLayout tl;
tl = (TableLayout) findViewById(R.id.tl);
for(int x = 0; x < 3; x++) {
// Create the table row
TableRow tr = new TableRow(this);
tr.setId(100+x);
TextView lblrecID = new TextView(this);
lblrecID.setId(200+x);
lblrecID.setText("test");
lblrecID.setGravity(Gravity.CENTER);
LayoutParams params = new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1f);
lblrecID.setLayoutParams(params);
tr.addView(lblrecID);// add the column to the table row here
TextView lblrecName = new TextView(this);
lblrecName.setId(200+x);// define id that must be unique
lblrecName.setText("test1");
lblrecName.setGravity(Gravity.CENTER);
lblrecName.setLayoutParams(params);
tr.addView(lblrecName); // add the column to the table row here
TableLayout tl1 = new TableLayout(this);
LayoutParams params1 = new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 3f);
tl1.setStretchAllColumns(true);
tl1.setLayoutParams(params1);
TableRow tr2 = new TableRow(this);
TextView lblrecID1 = new TextView(this);
lblrecID1.setId(200+x);
lblrecID1.setText("test2");
lblrecID1.setGravity(Gravity.CENTER);
tr2.addView(lblrecID1);// add the column to the table row here
TextView lblrecName1 = new TextView(this);
lblrecName1.setId(200+x);// define id that must be unique
lblrecName1.setText("test3");
lblrecName1.setGravity(Gravity.CENTER);
tr2.addView(lblrecName1); // add the column to the table row here
tl1.addView(tr2);
// finally add this to the table row
tr.addView(tl1);
//tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
}
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tl"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="*">
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#27160e" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="Project"
android:textColor="#fad06e"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="Project2"
android:textColor="#fad06e"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Cost"
android:textColor="#fad06e"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="2011"
android:textColor="#fad06e"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="2012"
android:textColor="#fad06e"
android:textSize="18sp"
android:textStyle="bold" />
</TableRow>
<TableRow>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="test" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="test" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="test" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="test" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="test" />
</TableRow>
<TableRow>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="test" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="test" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="test" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="test" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="test" />
</TableRow>