我试图以编程方式将两个视图添加为根子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);
结果是:底部视图不可见。
我试了一下:
- 改变第一
RecyclerView
的高度WRAP_CONTENT
(认为它可能填满整个空间隐藏底部布局),这是没有效果的。 取而代之的是
LayoutParams
设置为仰视图,到:container.addView(bottomView, lp);
但它也不能工作。
- 使用
LinearLayout
而不是RelativeLayout
容器,也是相同的行为。
我没有更多的想法是什么会导致这个问题,并通过查看类似的问题,没有任何工作。任何想法我做错了什么?
为什么所有的提问者正在使用setLayoutParams当你有正确那里有它应该通过的LayoutParams为2º参数调用addView ...... – Nanoc
你知道,当一件事不行,你正在尝试另一个。 我不知道有什么区别(我假设有),我提到我尝试了一切。在你去问别人之前,你不尝试你所知道的一切吗? – Jjang