Android 在表格行中对齐文本视图
我尝试在一个表格行中对齐3个文本视图,如下所示:Android 在表格行中对齐文本视图,android,textview,alignment,tablerow,Android,Textview,Alignment,Tablerow,我尝试在一个表格行中对齐3个文本视图,如下所示: |------------------------------------------------| | {TextView1} {TextView2} {TextView3} | |------------------------------------------------| // TextView 1, 2 Left aligned // TextView 3 Right aligned 此外,表格行应填充表格宽
|------------------------------------------------|
| {TextView1} {TextView2} {TextView3} |
|------------------------------------------------|
// TextView 1, 2 Left aligned
// TextView 3 Right aligned
此外,表格行应填充表格宽度
使用下面的代码,我只能做到这一点:
|------------------------------------------------|
| {TextView1} {TextView2} {TextView3} |
|------------------------------------------------|
I代码:
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
android:layout_height="wrap_content"
android:id="@+id/myTable"
android:layout_width="fill_parent"
>
</TableLayout>
第二次拍摄 我不确定您是以编程方式还是以XML或属性创建TableLayout,但听起来您想这样做 (爪哇语) (XML格式)
我觉得相对布局比较好。试试这个
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);
将文本视图放在TableRow中的RelativeLayout中,并使用相对对齐是一个更好的解决方案吗?在这种情况下,我觉得这没什么大不了的——不管你喜欢什么。如果您正在格式化LinearLayout而不是TableRow,我建议将整个内容设置为RelativeLayout以防止不必要的嵌套,但在这种情况下,您无法真正绕过它。使用带有参数FILL_PARENT的LinearLayout会导致正确的TextView消失。如果我使用params WRAP_CONTENT.K,也会发生同样的事情,疯狂的想法:我可以用RelativeLayouts而不是TableRows来填充TableLayout吗?为此,您将希望执行与编辑答案中几乎相同的操作,但将列1设置为“可收缩”(基本上将“可拉伸”替换为“可收缩”,将“2”替换为“1”)。从那里,您可以
setMaxLines(1)
/setEllipsize(TextUtils.TruncateAt.MIDDLE)
/等,并一直玩到它看起来像您想要的样子。我认为最好使用相对布局here@Labeeb你能详细说明一下吗?我刚试过你的原始代码,它对我来说很好。实际表格布局的代码是什么样子的?我计划将这些相对的行添加到我的表格中。是否需要为每一行指定它低于最后一行?
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);