以编程方式创建一个新的 TextView,然后将其显示在另一个 TextView 下方
String[] textArray={"one","two","asdasasdf asdf dsdaa"};
int length=textArray.length;
RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
for(int i=0;i<length;i++){
TextView tv=new TextView(getApplicationContext());
tv.setText(textArray[i]);
relativeParams.addRule(RelativeLayout.BELOW, tv.getId());
layout.addView(tv, relativeParams);
}
我需要做类似的事情..这样它就会显示在
one
two
asdfasdfsomething
屏幕上..
String[] textArray={"one","two","asdasasdf asdf dsdaa"};
int length=textArray.length;
RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
for(int i=0;i<length;i++){
TextView tv=new TextView(getApplicationContext());
tv.setText(textArray[i]);
relativeParams.addRule(RelativeLayout.BELOW, tv.getId());
layout.addView(tv, relativeParams);
}
I need to do something like that.. so it would display as
one
two
asdfasdfsomething
on the screen..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果使用RelativeLayout 并不重要,您可以使用LinearLayout,并执行以下操作:
这样做可以让您避免使用您尝试过的addRule 方法。您可以简单地使用 addView() 添加新的 TextView。
完整代码:
If it's not important to use a RelativeLayout, you could use a LinearLayout, and do this:
Doing this allows you to avoid the addRule method you've tried. You can simply use addView() to add new TextViews.
Complete code:
试试这个代码:
Try this code:
可以修改它以在不同的 TextView 中显示 String 数组的每个元素。
This can be modified to display each element of a String array in different TextViews.
您没有为文本视图分配任何 id,但使用
tv.getId()
将其作为参数传递给addRule
方法。尝试通过 tv.setId(int) 设置唯一 ID。您还可以使用垂直方向的 LinearLayout,这实际上可能更容易。如果没有必要的话,我更喜欢 LinearLayout 而不是relativelayout。
You're not assigning any id to the text view, but you're using
tv.getId()
to pass it to theaddRule
method as a parameter. Try to set a unique id viatv.setId(int)
.You could also use the LinearLayout with vertical orientation, that might be easier actually. I prefer LinearLayout over RelativeLayouts if not necessary otherwise.