Programmatically add view one below other in relative layout

前端 未结 3 668
一个人的身影
一个人的身影 2020-11-27 18:49

I want something like this programmatically:

view1 |  view2
view3 |  view4
----------------
view1 |  view2
view3 |  view4
----------------
view1 |  view2
vie         


        
相关标签:
3条回答
  • 2020-11-27 18:50
    ImageView imageView = new ImageView(this);
    
    
    // set the Drawable on the ImageView
    imageView.setImageDrawable(bmd);
    
    // center the Image
    imageView.setScaleType(ScaleType.CENTER);
    
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
    
    0 讨论(0)
  • 2020-11-27 18:56

    Important: Remember to set the ID for each view.

    RelativeLayout layout = new RelativeLayout(this);
    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    layout.setLayoutParams(layoutParams);
    
    RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    RelativeLayout.LayoutParams params3 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    RelativeLayout.LayoutParams params4 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    
    TextView tv1 = new TextView(this);
    tv1.setId(1);
    tv1.setText("textView1");
    
    TextView tv2 = new TextView(this);
    params2.addRule(RelativeLayout.RIGHT_OF, tv1.getId());
    tv2.setId(2);
    tv2.setText("textView2");
    
    TextView tv3 = new TextView(this);
    params3.addRule(RelativeLayout.BELOW, tv1.getId());
    tv3.setId(3);
    tv3.setText("textView3");
    
    TextView tv4 = new TextView(this);
    params4.addRule(RelativeLayout.RIGHT_OF, tv3.getId());
    params4.addRule(RelativeLayout.ALIGN_BOTTOM, tv3.getId());
    tv4.setId(4);
    tv4.setText("textView4");
    
    layout.addView(tv1, params1);
    layout.addView(tv2, params2);
    layout.addView(tv3, params3);
    layout.addView(tv4, params4);
    
    0 讨论(0)
  • 2020-11-27 19:00

    Instead of using multiple layout params as suggested in @Askilondz answer, you can use addRule and removeRule as below:

    RelativeLayout rl = new RelativeLayout(this.getContext());
    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,                                                                                     
                                LayoutParams.WRAP_CONTENT);
    TextView tv1 = new TextView(this);
    tv1.setId(View.generateViewId());
    tv1.setText("textView1");
    
    TextView tv2 = new TextView(this);
    tv2.setId(View.generateViewId());
    tv2.setText("textView2");
    lp.addRule(RelativeLayout.BELOW, tv1.getId());
    rl.addView(tv2, lp);
    lp.removeRule(RelativeLayout.BELOW);
    .
    .
    .
    

    If you are using SDK < 17, you have to create id's.xml file, as values -> ids.xml with structure like:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <item name="tv1" type="id" />
        <item name="tv2" type="id" />
    </resources>
    

    and set them in your code as:

    tv1.setId(R.id.tv1);
    tv2.setId(R.id.tv2);
    

    UPDATE It looks the Adding multiple rules for the same LayoutParams not working fine, anyhow, I'll keep my answer in case someone fine something usefull in it for him

    0 讨论(0)
提交回复
热议问题