Android RelativeLayout动态地在另一个RelativeLayout下面

时间:2017-03-10 14:50:29

标签: java android android-layout

我正在尝试动态构建由多个图像组成的相对布局。 这些相对布局将显示在先前相对布局的下方/下方。

我从两个相对布局(rl100和rl200)开始。 rl200低于rl100。 但rl200不低于rl100,在rl100上“堆积”。 你能救我吗?

我的代码:

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>

3 个答案:

答案 0 :(得分:1)

你在这里做错了

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

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

因为你没有在nIdBelow字段中传递id,所以应该在下面添加布局。传递上面的图像ID或使用垂直方向的线性布局。

答案 1 :(得分:1)

你应该使用这行代码在相对布局下面添加ID为200的相对布局,ID为100,对不对?

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

答案 2 :(得分:0)

感谢您的帮助。 我犯了不同的错误:

  1. RelativeLayout rL200 ...

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

  2. 正确版本: RelativeLayout rL200 = rlAdd(rLayout,200,100,-1);

    1. ImageButton imgBtn20x

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

    2. 正确版本:
      ImageButton imgBtn201 = imgBtnAdd(rL200,201,R.drawable.pineapple,-1,-1);

      imgBtn202的相同错误... imgBtn204

      1. activity_main.xml中
      2. 错误1和2是惊人的错误。

        RelativeLayout android中的第三个错误:id =&#34; @ + id / rlayout&#34; ...

        正确版本:

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