Android 用户界面控件';宽度
我有以下UI层次结构 滚动查看-->表格布局-->行[每行有TextView+EditText] 问题 在一行内,每个视图不应超过父视图(TableRow)宽度的50%。如果创建了一个EditText来输入,比如用户名,最大字符数为N;然后EditText的宽度理想情况下应该正好是N个字符长,但不能超过50%的限制 我尝试了许多排列和组合,但到目前为止都没有成功 我必须按照项目要求以编程方式完成Android 用户界面控件';宽度,android,android-layout,android-edittext,Android,Android Layout,Android Edittext,我有以下UI层次结构 滚动查看-->表格布局-->行[每行有TextView+EditText] 问题 在一行内,每个视图不应超过父视图(TableRow)宽度的50%。如果创建了一个EditText来输入,比如用户名,最大字符数为N;然后EditText的宽度理想情况下应该正好是N个字符长,但不能超过50%的限制 我尝试了许多排列和组合,但到目前为止都没有成功 我必须按照项目要求以编程方式完成 谢谢。尝试使用如下布局权重: <TableRow android:id="@+id/t
谢谢。尝试使用如下布局权重:
<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>
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);
我看到了您的评论,请询问答案。我认为您可以将edittext或textview的宽度设置为包裹内容。并将这些控件的“最大宽度”属性设置为屏幕宽度的50%。然后它的宽度将不会超过父级宽度的50%。为了使我自己的布局有效,我不得不放弃TableLayout。TableRows中的布局参数似乎无效。线性布局看起来更灵活 现在我有了下面的布局层次结构,它可以工作了
滚动查看-->LinearLayout而不是TableLayout-->[单个字段封装在LinearLayout而不是TableRows中]在此处显示代码。因此,如果您想限制字符,任何人都可以帮助您,您可以使用android:maxLength=“10”@AndroidTechMe我的代码就是“ask”所建议的。谢谢。尝试了这个方法,但它将每个视图的宽度精确地固定为50%。但是,它应该是上限,如果在EditText中只输入5个字符,那么它应该是5个字符长。在这种情况下,50%的空间太长,看起来不好。不工作。你试过了吗?setXXXWidth方法似乎无效。