当一个视图以编程方式与底部对齐时,在两个视图之间添加视图

时间:2015-11-09 10:23:19

标签: android relativelayout layoutparams

我有一个相对布局,其中包含我想要放置在两个其他相对布局之间的滚动视图。其中一个布局与父底部对齐。我希望它能保持在最低点。

这是我的代码:

RelativeLayout.LayoutParams formRenderParams = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        mainLayout.setLayoutParams(formRenderParams);
        formRenderParams.addRule(RelativeLayout.BELOW, pageButtons.getId());

        //relative is the relative layout that has been aligned to bottom in previous code
        RelativeLayout.LayoutParams p = (RelativeLayout.LayoutParams) relative.getLayoutParams();
        p.addRule(RelativeLayout.BELOW, r_scroll.getId());
        relative.setLayoutParams(p);


        mainLayout.addView(r_scroll, formRenderParams);

pageButtons是布局A,r_scroll是布局B,相对布局是布局C.

问题是C消失了。它会随着此代码消失,或者B重叠。我想要的是B占用剩余的空间,但没有重叠。

此处还有一张可视化显示问题的图片:

enter image description here

有关正在发生的事情的任何想法?任何帮助,将不胜感激。谢谢。

3 个答案:

答案 0 :(得分:1)

只需遵循此布局的模式

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <RelativeLayout
        android:id="@+id/a"
        android:layout_alignParentTop="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <RelativeLayout
        android:id="@+id/b"
        android:layout_below="@+id/a"
        android:layout_above="@+id/c"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        </ScrollView>
    </RelativeLayout>
    <RelativeLayout
        android:id="@+id/c"
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</RelativeLayout>

答案 1 :(得分:1)

只需添加RelativeLayout.LayoutParams formRenderParams此规则:

formRenderParams.addRule(RelativeLayout.ABOVE, relative .getId());

并更改

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

p.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);

避免布局中的循环依赖。

修改

RelativeLayout.LayoutParams formRenderParams = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
r_scroll.setBackgroundColor(getResources().getColor(android.R.color.black)); // this is just to test the guess

答案 2 :(得分:0)

试试这个

    RelativeLayout rlmain= (RelativeLayout) findViewById(R.id.rlMain);

    RelativeLayout rA=new RelativeLayout(MainActivity.this);
    RelativeLayout.LayoutParams layoutA = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    rA.setId(1);
    rA.setBackgroundColor(getResources().getColor(R.color.blue));
    layoutA.addRule(RelativeLayout.ALIGN_PARENT_TOP, rlmain.getId());
    rA.setLayoutParams(layoutA);

    TextView tvA=new TextView(MainActivity.this);
    RelativeLayout.LayoutParams tvParamA = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    tvA.setText("This is Layout 1");
    tvA.setLayoutParams(tvParamA);
    rA.addView(tvA);

    RelativeLayout rC=new RelativeLayout(MainActivity.this);
    RelativeLayout.LayoutParams layoutC = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    layoutC.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,rlmain.getId());
    rC.setId(3);
    rC.setLayoutParams(layoutC);
    rC.setBackgroundColor(getResources().getColor(R.color.gray));

    TextView tvC=new TextView(MainActivity.this);
    RelativeLayout.LayoutParams tvParamC = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    tvC.setText("This is Layout 3");
    tvC.setLayoutParams(tvParamC);
    rC.addView(tvC);

    RelativeLayout rB=new RelativeLayout(MainActivity.this);
    RelativeLayout.LayoutParams layoutB = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
    layoutB.addRule(RelativeLayout.BELOW, rA.getId());
    layoutB.addRule(RelativeLayout.ABOVE, rC.getId());
    rB.setId(2);
    rB.setLayoutParams(layoutB);
    rB.setBackgroundColor(getResources().getColor(R.color.orange));

    ScrollView scroll=new ScrollView(MainActivity.this);
    RelativeLayout.LayoutParams scrollB = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
    scroll.setLayoutParams(scrollB);
    rB.addView(scroll);

    rlmain.addView(rA);
    rlmain.addView(rB);
    rlmain.addView(rC);
上面代码中的rlMain是在xml文件中声明的RelativeLayout。

希望这可以帮到你!