Setting up RelativeLayout in java code

最后都变了- 提交于 2019-12-01 03:58:34

Your TextViews don't have an id (by default the id is -1)... put this after their initialization:

text1.setId(1111); // 1111 is just an example,
text2.setId(2222); // just make sure the id are unique

I don't think you are looking to layout the text1 view below the RelativeLayout since you added all your views to it as children, right? Try removing the first rule; that rule is asking the text view to be below the same view it is in.

EDIT: Also a help is explicitly setting the id of the view you are laying out relative to.

So here:

text1.setId(2);
p.addRule(RelativeLayout.BELOW,2);

you can use xml layout for this :

in relative layout u set the first textview and assign it some id fot the next text view we can assign parameter android:layout_below="id of above text view" in this way we get 2nd text view below 1st text view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!