以编程方式将项目添加到相对布局
我一直在到处寻找这个问题的答案。我是 Android 新手,尝试通过 java 而不是 xml 以编程方式将项目添加到相对布局。我创建了一个测试类来尝试一下,但项目不断堆叠而不是正确格式化。我现在只想将一个 TextView 放在另一个下面(最终我将使用参数的左侧和右侧,但我从简单开始。我缺少什么?
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ScrollView sv = new ScrollView(this);
RelativeLayout ll = new RelativeLayout(this);
ll.setId(99);
sv.addView(ll);
TextView tv = new TextView(this);
tv.setText("txt1");
tv.setId(1);
TextView tv2 = new TextView(this);
tv2.setText("txt2");
tv2.setId(2);
RelativeLayout.LayoutParams lay = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lay.addRule(RelativeLayout.ALIGN_PARENT_TOP);
ll.addView(tv, lay);
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
p.addRule(RelativeLayout.ALIGN_BOTTOM, tv.getId());
ll.addView(tv2, p); this.setContentView(sv);};
I have been searching everywhere for an answer to this question. I'm new to Android and attempting to add items to a relative layout programmatically through java instead of xml. I have created a test class to try it out but the items keep stacking instead of formatting correctly. I simply want one TextView under the other for now (eventually I will use the left of and right of parameters but I am starting simple. What am I missing?
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ScrollView sv = new ScrollView(this);
RelativeLayout ll = new RelativeLayout(this);
ll.setId(99);
sv.addView(ll);
TextView tv = new TextView(this);
tv.setText("txt1");
tv.setId(1);
TextView tv2 = new TextView(this);
tv2.setText("txt2");
tv2.setId(2);
RelativeLayout.LayoutParams lay = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lay.addRule(RelativeLayout.ALIGN_PARENT_TOP);
ll.addView(tv, lay);
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
p.addRule(RelativeLayout.ALIGN_BOTTOM, tv.getId());
ll.addView(tv2, p); this.setContentView(sv);};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这条线意味着 tv2 的底部与 tv 的底部对齐 - 换句话说,它们将相互覆盖。您想要的属性大概是
RelativeLayout.BELOW
。不过,我强烈建议使用 xml 来代替。This line means that the the bottom of tv2 is aligned with the bottom of tv- in other words, they will cover each other up. The property you want is presumably
RelativeLayout.BELOW
. However, I strongly recommend using xml for this instead.使用:
Use:
你错过了很多东西,首先 ScrollView 没有措施,用 LayoutParams.FILL_PARENT 或 WRAP_CONTENT 设置它,其次;它的TextView1放错了位置,所以TextView2,用lay.addRule设置TextView1位置(RelativeLayout.ALIGN_PARENT_TOP,RelativeLayout.TRUE);
you are missing various things, first the ScrollView has no measures, set it with LayoutParams.FILL_PARENT or WRAP_CONTENT, second; it TextView1 are misplaced so TextView2, set TextView1 position with lay.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);