【发布时间】:2016-05-04 20:07:13
【问题描述】:
我有一个相对布局和“n”个 textviews 和 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 但没有帮助。任何帮助都会很棒!谢谢
【问题讨论】:
-
这里为什么不用线性布局?
-
你想让我使用线性布局而不使用参数吗?它是动态生成的。我怎样才能有线性布局?它将动态生成大约 100 个编辑文本@SohailZahid
-
如果您不使用相对布局属性,那么只需使用带方向的线性布局。
-
出于性能考虑,您可以在单个循环中添加 Editext 和 TextView。
-
如果我使用水平方向的线性布局,那么我的输出将类似于 edittext edittext edittext............. @SohailZahid
标签: android alignment layoutparams