开发者

Programmatically adding items to a relative layout

开发者 https://www.devze.com 2023-02-07 23:21 出处:网络
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

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);};


    p.addRule(RelativeLayout.ALIGN_BOTTOM, tv.getId());

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:

p.addRule(RelativeLayout.BELOW, tv.getId());


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);

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ScrollView sv = new ScrollView(this);
    RelativeLayout ll = new RelativeLayout(this);
    ll.setId(99);

    sv.addView(ll, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

    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, RelativeLayout.TRUE);
    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);};
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号