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

Adding a view between 2 views when one view is aligned to the bottom programmatically

我有一个相对布局,其中包含我想放置在其他 2 个相对布局之间的滚动视图。这些布局之一与父底部对齐。我希望它留在底部。

这是我的代码:

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,relative 是布局 C。

问题是C消失了。它要么随此代码消失,要么 B 与它重叠。我想要的是 B 占据剩余的 space,但没有重叠。

这里还有一张图片直观地展示了问题:

对发生的事情有什么想法吗?任何帮助,将不胜感激。谢谢你。

按照这个布局的模式就行了

<?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>

只需添加到 formRenderParamsRelativeLayout.LayoutParams 这条规则:

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

试试这个

    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。

希望对您有所帮助!