在 TableLayout 中以编程方式分配行中的列权重
我只想在表中定义一行。对于这一行,我想添加 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
感谢您的帮助。
I want to define just one row in my table. To this row, I want to add 3 columns, with the following widths:
textView1 : should be as wide as its contents
viewDivider: should be 2px wide
TextView2: should occupy the remaining area of the tablerow.
However, I am not able to achieve the above layout, programatically. Here is the code:
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));
}
}
And here's 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>
I am facing thefollowing issues with the above layout:
1) textView1 occupies more width that its contents.
2) viewDivider occupies a lot of width and is not restriced to 2px
Thanks for the help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用 setWidth() 方法设置宽度。
供参考
Try Using setWidth() method for setting width.
For Reference