在TableRow中alignmentTextView

我想在一个表格中alignment3个文本视图,如下所示:

|------------------------------------------------| | {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); 

正确的文本视图不适合右侧,并且表格行不填充表格宽度。 我需要在文字视图上使用权重吗?

编辑:添加TableLayout:

 <TableLayout android:layout_height="wrap_content" android:id="@+id/myTable" android:layout_width="fill_parent" > </TableLayout> 

Solutions Collecting From Web of "在TableRow中alignmentTextView"

第二枪

我不确定您是以编程方式创buildTableLayout还是使用XML或属性,但听起来像是想要

(在Java中)

 myTable.setColumnStretchable(2, true); 

(用XML)

 android:stretchColumns="2" 

我认为相对布局更好。 尝试这个

  RelativeLayout tr = new RelativeLayout(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]); tr.addView(tvLeft); RelativeLayout.LayoutParams relativeLayoutParamsCenter = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); relativeLayoutParamsCenter.addRule(RelativeLayout.RIGHT_OF, tvLeft.getId()); tr.addView(tvCenter,relativeLayoutParamsCenter); RelativeLayout.LayoutParams relativeLayoutParamsRight = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); relativeLayoutParamsRight.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE); tr.addView(tvRight,relativeLayoutParamsRight); myTable.addView(tr);