2015-11-06 133 views
1

我试图以编程方式将两个视图添加为根子RelativeLayout的子项,当一个视图在另一个视图下方时。相对视图内以编程方式编排布局下面

这里的根视图(也驻留在另一个CoordinatorLayout,但我不认为这是相关的):

<RelativeLayout 
    android:id="@+id/container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior"/> 

现在,这里是两个布局我想以编程方式添加一个:

<android.support.v7.widget.RecyclerView  
    android:id="@+id/recycler_view" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior"/> 

,另一个:

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <ImageView 
     android:id="@+id/icon" 
     android:layout_width="30dp" 
     android:layout_height="30dp" 
     android:layout_alignParentLeft="true" 
     android:background="@drawable/ic_group_members"/> 

    <TextView 
     android:id="@+id/text" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/icon" 
     android:layout_centerVertical="true" 
     android:layout_marginLeft="15dp" 
     android:layout_toRightOf="@+id/icon" 
     android:textSize="18sp" 
     android:textStyle="bold" 
     android:text="@string/members_title"/> 

</RelativeLayout> 

添加此与此代码:

RelativeLayout container = (RelativeLayout)findViewById(R.id.container); 
container.addView(topView); 
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
lp.addRule(RelativeLayout.BELOW, bottomView.getId()); 
bottomView.setLayoutParams(lp); 
container.addView(bottomView); 

结果是:底部视图不可见。

我试了一下:

  1. 改变第一RecyclerView的高度WRAP_CONTENT(认为它可能填满整个空间隐藏底部布局),这是没有效果的。
  2. 取而代之的是LayoutParams设置为仰视图,到:

    container.addView(bottomView, lp); 
    

但它也不能工作。

  1. 使用LinearLayout而不是RelativeLayout容器,也是相同的行为。

我没有更多的想法是什么会导致这个问题,并通过查看类似的问题,没有任何工作。任何想法我做错了什么?

+0

为什么所有的提问者正在使用setLayoutParams当你有正确那里有它应该通过的LayoutParams为2º参数调用addView ...... – Nanoc

+0

你知道,当一件事不行,你正在尝试另一个。 我不知道有什么区别(我假设有),我提到我尝试了一切。在你去问别人之前,你不尝试你所知道的一切吗? – Jjang

回答

0

您的第一个视图是RecyclerView,它是一个可滚动的视图。如果将高度wrap_content设置为RecyclerView/ListView,则无关紧要。在所有情况下,除非您为RecyclerView设置了一个特定的高度,明显小于设备的高度,否则它将填满整个屏幕。 RecyclerView下面的第二个视图会显示出来。以下是您可以尝试的方法:

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, HEIGHT_OF_VIEW); 
recyclerView.setLayoutParams(params); 
+0

嗯,我不知道前面的回收站视图的大小是多少,因为只有在运行时,适配器项才会被启动。除非我以编程方式设置,提前,适配器项目的大小...但这种感觉错误 – Jjang

0

LinearLayout方法应该可以工作。

只是介意:
1)集装箱的LinearLayout应该有layout_height MATCH_PARENT
2)RecyclerView应该有0 layout_height和1

layout_weight相反,如果你想保持RelativeLayout的方法试试:
1 )带有规则RelativeLayout.ALIGN_PARENT_BOTTOM的BottomLayout
2)带规则RelativeLayout的RecyclerView。以上

例第2做法:

RelativeLayout containerLayout = (RelativeLayout) findViewById(R.id.container); 

RelativeLayout bottomLayout = new RelativeLayout(this); 
bottomLayout.setId(R.id.bottom_id); 
RelativeLayout.LayoutParams bottomLayoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); 
      bottomLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); 
bottomLayout.setLayoutParams(bottomLayoutParams); 
bottomLayout.setBackgroundColor(Color.RED); 
TextView bottomTextView = new TextView(this); 
bottomTextView.setText("Bottom Layout"); 
bottomLayout.addView(bottomTextView); 
containerLayout.addView(bottomLayout); 

RecyclerView recyclerView = new RecyclerView(this); 
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); 
params.addRule(RelativeLayout.ABOVE, R.id.bottom_id); 
recyclerView.setLayoutParams(params); 
recyclerView.setBackgroundColor(Color.BLUE); 
containerLayout.addView(recyclerView); 
+0

第一种方法的结果是只有底部视图是可见的。 第二种方法导致这两个视图可见和重叠... – Jjang

+0

我添加了一个你问我的RelativeLayout方法的例子 – ctarabusi

+0

我猜这是行不通的,因为我在一个CoordinatorLayout里面(基本上我有一个灵活的空间和一个图像试图在图片上添加额外的文字,但它不起作用) – Jjang

相关问题