我想在我的表中只定义一行。在这一行中,我想添加3列,其宽度如下:
textView1:应该与其内容一样宽
viewDivider:应为2px宽
TextView2:应占据tablerow的剩余区域。
但是,我无法以编程方式实现上述布局。这是代码:
public class Copy_2_of_Test extends Activity {
TableLayout tableLayout = null;
TextView textView1 = null;
TextView textView2 = null;
View viewDivider = null;
TableLayout tab = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView (R.layout.test);
tableLayout = (TableLayout)this.findViewById(R.id.mainTable);
addViews();
}
private void addViews() {
// table row with layout params of type TableLayout.LayoutParams
TableRow tr = new TableRow(this);
tr.setLayoutParams(new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
// Textview 1 with layout params of type TableRow.LayoutParams
textView1 = new TextView(this);
textView1.setText("Value1");
textView1.setTextColor(Color.BLACK);
textView1.setBackgroundColor(Color.YELLOW);
textView1.setLayoutParams(new TableRow.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.FILL_PARENT));
tr.addView(textView1);
viewDivider = new View (this);
viewDivider.setLayoutParams(new TableRow.LayoutParams(
2,
LayoutParams.FILL_PARENT));
viewDivider.setBackgroundColor(Color.MAGENTA);
tr.addView(viewDivider);
// Textview 2 with layout params of type TableRow.LayoutParams
textView2 = new TextView(this);
textView2.setText("Value2 Value2 Value2 Value2 ");
textView2.setTextColor(Color.BLACK);
textView2.setBackgroundColor(Color.YELLOW);
textView2.setLayoutParams(new TableRow.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.FILL_PARENT));
tr.addView(textView2);
// Add row to TableLayout.
tableLayout.addView(tr,new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
}
}
这是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:stretchColumns="*"
android:background="@color/black"
android:id="@+id/mainTable">
</TableLayout>
我正面临上述布局的以下问题:
1)textView1占用的内容宽度更多。
2)viewDivider占用很多宽度,不限于2px
感谢您的帮助。