【问题标题】:Adding a view between 2 views when one view is aligned to the bottom programmatically当一个视图以编程方式与底部对齐时,在两个视图之间添加一个视图
【发布时间】:2016-02-09 23:21:22
【问题描述】:

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

这是我的代码:

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 为 Layout A,r_scroll 为 Layout B,relative 为 Layout C。

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

还有一张图片可以直观地显示问题:

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

【问题讨论】:

    标签: android android-relativelayout layoutparams


    【解决方案1】:

    试试这个

        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。

    希望对您有所帮助!

    【讨论】:

    • 谢谢。我将以此为例。
    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      只需在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
      

      【讨论】:

      • 感谢您的帮助,但没有奏效。 r_scroll 现在根本没有出现,但 relative 确实显示了。也许我没有显示足够的代码;我以为我只会展示正在提升的部分。
      • 这只是一个猜测,但可能是因为此时 ScrollView 内容为空。尝试将 r_scroll 的高度设置为 MATCH_PARENT 而不是 WRAP_CONTENT。我已经用这些细节更新了答案。
      猜你喜欢
      • 1970-01-01
      • 2021-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-11
      • 1970-01-01
      • 2012-06-01
      • 1970-01-01
      相关资源
      最近更新 更多