我完全不知道为什么test,test1,test2,test3不能和第一行对齐。第一行是指test,2012,test,test。我希望下一行紧跟在表中的第一行之后。请帮帮忙。在进阶时谢谢。
xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/tl"
android:weightSum="5"
>
<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:textStyle="bold"
android:textSize="18sp"
android:gravity="center"
android:textColor="#fad06e"
android:layout_weight="1"
android:text="Project" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textStyle="bold"
android:textSize="18sp"
android:gravity="center"
android:textColor="#fad06e"
android:layout_weight="1"
android:text="Project2" />
<TableLayout
android:layout_weight="3"
android:layout_marginRight="4dp"
android:stretchColumns="*"
>
<TableRow>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="18sp"
android:textColor="#fad06e"
android:text="Cost"
android:gravity="center"
android:layout_span="2" />
</TableRow>
<TableRow
>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textStyle="bold"
android:textSize="18sp"
android:textColor="#fad06e"
android:text="2011" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textStyle="bold"
android:textSize="18sp"
android:textColor="#fad06e"
android:text="2012" />
</TableRow>
</TableLayout>
</TableRow>
<TableRow>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:text="test" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:text="test" />
<TableLayout android:stretchColumns="*"
android:layout_weight="3">
<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" />
</TableRow>
</TableLayout>
</TableRow>
</TableLayout>代码:
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));
}结果

发布于 2014-08-01 01:12:31
好的。我不明白你想要什么。是不是像这样的布局?
<?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>
https://stackoverflow.com/questions/25063984
复制相似问题