我有以下UI层次结构
滚动视图->TableLayout->每行都有TextView+EditText
问题在一行中,每个视图不应超过父视图宽度(TableRow)的50%。如果创建了一个EditText来输入,假设用户名中有最大字符N;那么理想情况下,EditText的宽度应该是N个字符长,但不能超过50%的限制。
我尝试过很多排列和组合,但到目前为止还没有得到正确的结果。
我必须按照项目要求以编程的方式来完成它。
谢谢。
发布于 2012-04-03 05:02:39
为了使我自己的布局有效,我不得不放弃TableLayout。在TableRows中布局参数似乎是无效的。LinearLayout看起来更灵活。
现在我有了下面的布局层次结构,它可以工作。
滚动视图-> LinearLayout而不是TableLayout->在LinearLayout中而不是TableRows中封装的单个字段
发布于 2012-02-01 05:53:19
尝试使用这样的布局权重:
<TableRow
android:id="@+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textview1"
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="1">
</TextView>
<EditText
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="1" >
</EditText>
</TableRow>若要以编程方式执行此操作,请尝试使用LayoutParams来实现如下所示:
TableLayout table = (TableLayout) findViewById(R.id.TableLayout1);
TableRow tr = new TableRow(this);
TableLayout.LayoutParams trparams = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT);
tr.setLayoutParams(trparams);
textview1 = new TextView(this);
edittext1 = new EditText(this);
TableRow.LayoutParams fieldparams = new TableRow.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f);
tr.addView(textview1, fieldparams);
tr.addView(edittext1, fieldparams);
table.addView(tr);发布于 2012-02-01 11:32:15
我看到你的评论来问问题的答案。我认为你可以把编辑文本或文本视图的宽度设置为content.and,设置这些控件的最大宽度属性为screen.then的50%,它的宽度不会超过父控件宽度的50%。
https://stackoverflow.com/questions/9091365
复制相似问题