我有一个相对的布局和'n'个文本视图和edittext。我想要作为的对齐
TextView EditText
Button
TextView EditText
Button
TextView EditText
Button
TextView EditText
Button
..........
我试过使用布局参数,但我得到了奇怪的结果。这是我的代码,
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.linearlayoutid);
final TextView[] myTextViews = new TextView[41];
for (int i = 0; i < 41; i++) {
// create a new textview
rowTextView = new TextView(this);
RelativeLayout.LayoutParams paramsA = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
paramsA.addRule(RelativeLayout.RIGHT_OF, rowTextView.getId());
rowTextView.setId(i);
// add the textview to the layout
relativeLayout.addView(rowTextView, paramsA);
myTextViews[i] = rowTextView;
}
对于编辑文本。。
final EditText[] editTextarray = new EditText[41];
for (int i = 0; i < 41; i++) {
// create a new textview
final EditText editText = new EditText(this);
RelativeLayout.LayoutParams paramsB = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
editText.setText("edit");
editText.setCursorVisible(true);
relativeLayout.addView(editText, paramsB);
editTextarray[i] = editText;
}
我已经应用了这样的layout_params,但它没有帮助。任何帮助都会很棒!!感谢
添加布局
layout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/ll"
>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="textview"
/>
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Edittex"
/>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/ll"
android:layout_centerHorizontal="true"
android:text="button"
/>
</RelativeLayout>
在运行时添加此布局:
View view;
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for (int i = 0; i < 40; i++) {
view= inflater.inflate(R.layout.layout, null);
linearLayout.addView(view);
}
我建议您使用自定义布局并以编程方式对其进行扩展。
创建一个由TextView
、EditText
和Button
组成的布局,并对其进行充气
View v=LayoutInflater.from(this).inflate(R.layout.customLayout,null);
并使用变量v
来表示TextView
、EditText
和Button
。
你的customLayout.xml
可以是这样的:
<LinearLayout android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
以xml中具有垂直方向的LinearLayout
为例,将该视图添加到该布局中以创建不同的视图。
for (int i = 0; i < 20; i++) {
View v = inflater.inflate(R.layout.customLayout, null);
ll.addView(v);
}
Anusha尝试使用linearlayout
不过,若你们的设计是相同的,你们应该使用列表视图的最佳方法。
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearlayoutid);
final TextView[] myTextViews = new TextView[41];
final EditText[] editTextarray = new EditText[41];
for (int i = 0; i < 41; i++) {
// create a new textview
rowTextView = new TextView(this);
myTextViews[i] = rowTextView;
linearLayout.addView(myTextViews[i]);
final EditText editText = new EditText(this);
editText.setText("edit");
editText.setCursorVisible(true);
editTextarray[i] = editText;
linearLayout.addView(editTextarray[i]);
}
我建议您为这个需求实现ListView-
根据您当前的方法,您最终将为每个按钮设置多个onClicklistener-如果该按钮将选择与textview/edittext相关联的输入数据-您最终将编写许多数组调用和填充
Listview-点击任何项目,你可以在ClickListener上启动一个,在那里你可以区分
PS:错误的设计是在textview/edittext下面重复按钮,如果按钮需要执行相同/类似的功能,比如保存-只有textview/eedittext的列表视图-工具栏上或表单下面的某个常见按钮操作。