我正在尝试像这样在一个表格行中对齐 3 个 TextView :
|------------------------------------------------|
| {TextView1} {TextView2} {TextView3} |
|------------------------------------------------|
// TextView 1, 2 Left aligned
// TextView 3 Right aligned
此外,表格行应填满表格宽度。
用下面的代码我只能做到这一点:
|------------------------------------------------|
| {TextView1} {TextView2} {TextView3} |
|------------------------------------------------|
我代码:
TableRow tr = new TableRow(myActivity.this);
TextView tvLeft = new TextView(myActivity.this);
tvLeft.setText(values[0]);
TextView tvCenter = new TextView(myActivity.this);
tvCenter.setText(values[1]);
TextView tvRight = new TextView(myActivity.this);
tvRight.setText(values[2]);
tvRight.setGravity(Gravity.RIGHT);
tr.addView(tvLeft);
tr.addView(tvCenter);
tr.addView(tvRight);
myTable.addView(tr);
右侧的 TextView 没有向右倾斜,并且表格行没有填满表格宽度。我需要在 TextView 上使用权重吗?
编辑:添加 TableLayout:
<TableLayout
android:layout_height="wrap_content"
android:id="@+id/myTable"
android:layout_width="fill_parent"
>
</TableLayout>
最佳答案
第二次拍摄
我不确定您是以编程方式还是以 XML 创建 TableLayout 或属性是什么,但听起来您想要
(在 Java 中)
myTable.setColumnStretchable(2, true);
(以 XML 格式)
android:stretchColumns="2"
关于android - 在 TableRow 中对齐 TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6367494/