简体   繁体   中英

Android RelativeLayout below another RelativeLayout dynamically

I'm trying to dynamically build relative layouts consisting of several images. Theses relative layouts will be display below/rigth of previous relative layouts.

I'm starting with two relative layouts (rl100 and rl200). rl200 is below rl100. But rl200 isn't below rl100, is "stacked" over rl100. Can you help me ?


My code :

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RelativeLayout rLayout = (RelativeLayout) findViewById(R.id.rlayout);

        RelativeLayout rL100 = rlAdd(rLayout,100,-1,-1);
        ImageButton imgBtn101 = imgBtnAdd(rL100,101,R.drawable.apricot,-1,-1);
        ImageButton imgBtn102 = imgBtnAdd(rL100,102,R.drawable.banana,-1,101);
        ImageButton imgBtn103 = imgBtnAdd(rL100,103,R.drawable.cherry,101,-1);
        ImageButton imgBtn104 = imgBtnAdd(rL100,104,R.drawable.strawberry,102,103);

        RelativeLayout rL200 = rlAdd(rLayout,200,100,-1);
        ImageButton imgBtn201 = imgBtnAdd(rL100,201,R.drawable.pineapple,-1,-1);
        ImageButton imgBtn202 = imgBtnAdd(rL100,202,R.drawable.pineapple,-1,201);
        ImageButton imgBtn203 = imgBtnAdd(rL100,203,R.drawable.pineapple,201,-1);
        ImageButton imgBtn204 = imgBtnAdd(rL100,204,R.drawable.pineapple,202,203);
    }


    private RelativeLayout rlAdd(RelativeLayout parentContainer, int nId, int nIdBelow,
                                 int nIdRightOf) {
        RelativeLayout rLayout = new RelativeLayout(this);
        rLayout.setId(nId);
        RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        if (nIdBelow != -1) {
            rlp.addRule(RelativeLayout.BELOW, nIdBelow);
        }
        if (nIdRightOf != -1) {
            rlp.addRule(RelativeLayout.RIGHT_OF, nIdRightOf);
        }
        rLayout.setLayoutParams(rlp);
        parentContainer.addView(rLayout);
        return rLayout;
    }

    private ImageButton imgBtnAdd(RelativeLayout ParentContainer, int ImgId, int ResDrawable,
                                  int imgIdBelow, int imgIdRightOf) {

        ImageButton imgBtn = new ImageButton(this);
        imgBtn.setId(ImgId);
        imgBtn.setImageResource(ResDrawable);
        ParentContainer.addView(imgBtn);

        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) imgBtn.getLayoutParams();

        if (imgIdBelow != -1) {
            lp.addRule(RelativeLayout.BELOW, imgIdBelow);
        }
        if (imgIdRightOf != -1) {
            lp.addRule(RelativeLayout.RIGHT_OF, imgIdRightOf);
        }
        imgBtn.setLayoutParams(lp);

        return imgBtn;
    }
}

activity_main.xml :

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"    >
    <RelativeLayout android:id="@+id/rlayout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal">
    </RelativeLayout>
</ScrollView>

You are doing wrong over here

RelativeLayout rL200 = rlAdd(rLayout,100,-1,-1);

// From method
if (nIdBelow != -1) {
     rlp.addRule(RelativeLayout.BELOW, nIdBelow);
}

As you are not passing id in nIdBelow field how it is supposed to add layout below. pass above image id or use Linear Layout with vertical orientation.

您应该使用此行代码将ID为200的相对布局添加到ID为100的相对布局下面,对吗?

RelativeLayout rL200 = rlAdd(rLayout, 200, 100, -1);

Thanks for your help. I make differents errors :

  1. RelativeLayout rL200...

    RelativeLayout rL200 = rlAdd(rLayout, 200, 100, -1);

Correct version : RelativeLayout rL200 = rlAdd(rLayout,200,100,-1);

  1. ImageButton imgBtn20x

    ImageButton imgBtn201 = imgBtnAdd(rL100,201,R.drawable.pineapple,-1,-1);

Correct version :
ImageButton imgBtn201 = imgBtnAdd(rL200,201,R.drawable.pineapple,-1,-1);

Same error for imgBtn202 ... imgBtn204

  1. activity_main.xml

Errors 1 and 2 are stunning errors.

Third error in RelativeLayout android:id="@+id/rlayout" ...

Correct version :

<RelativeLayout android:id="@+id/rlayout" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="match_parent">
</RelativeLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM