Android 在recyclerview项中不可见高度匹配父项的视图
下面是recyclerview项的布局。我想在每个布局项的左侧创建一个垂直视图,使其与项的高度匹配。我使用了下面的代码-Android 在recyclerview项中不可见高度匹配父项的视图,android,android-layout,android-recyclerview,android-view,Android,Android Layout,Android Recyclerview,Android View,下面是recyclerview项的布局。我想在每个布局项的左侧创建一个垂直视图,使其与项的高度匹配。我使用了下面的代码- <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" android:background="@drawable/f
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@drawable/foodvitebackground">
<TextView
android:id="@+id/invitename"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18dp"
android:textColor="@color/adsd"
android:text="fasf"
android:layout_marginTop="10dp"
android:layout_marginLeft="15dp"/>
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/invitename"
android:layout_marginTop="8dp"
android:layout_marginLeft="15dp"
android:layout_toLeftOf="@+id/profileimage"
android:gravity="left"
android:textColor="@color/foodvite_invitename"
android:textSize="20dp"
android:text="Fill-a Pita"/>
<TextView
android:id="@+id/jJoinTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/name"
android:layout_marginTop="8dp"
android:layout_marginLeft="15dp"
android:textSize="12dp"
android:text="Today,8:30PM"
android:textColor="@color/joinTime"/>
<com.mikhaellopez.circularimageview.CircularImageView
android:id="@+id/profileimage"
android:layout_width="@dimen/hundred_dp"
android:layout_height="@dimen/hundred_dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="15dp"
android:scaleType="fitCenter"/>
<TextView
android:id="@+id/invitescount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="20"
android:textColor="@color/white"
android:paddingLeft="3dp"
android:paddingRight="3dp"
android:background="@drawable/ret"
android:layout_marginBottom="15dp"
android:layout_marginRight="15dp"
android:layout_alignBottom="@+id/message"
android:layout_alignRight="@+id/profileimage"
android:layout_alignEnd="@+id/profileimage"/>
<View
android:id="@+id/leftbar"
android:layout_width="6dp"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:background="@drawable/foodviterespoded"/>
</RelativeLayout>
在上面的代码中,我看不到id为leftbar的视图可见。但是,如果我将该视图的高度从match_parent更改为某些数字值,如160dp,那么我会看到左侧条形视图可见。为什么会发生这种情况。我不想在数字值bcoz中设置高度,我希望我的视图拉伸到项目的完整高度,并且项目的高度可能是在不同的设备中不同。您可以尝试将android:layout\u alignParentTop=“true”和
android:layout\u alignParentBottom=“true”
设置为id为“leftbar”的视图吗
更新:
试试这个
<View
android:id="@+id/leftbar"
android:layout_width="6dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="@+id/invitename"
android:layout_alignBottom="@+id/jJoinTime"
android:background="@drawable/foodviterespoded"/>
您正在为正在删除leftbar视图的父relativelayout指定高度作为包裹内容。尝试将父级相对布局高度设置为匹配父级在视图持有者的构造函数中计算回收器视图的单元格高度
itemView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
height = itemView.getMeasuredHeight();
使用onBindViewHolder()中的上述计算高度设置分隔器的高度
RecyclerView布局项不应具有匹配父项高度,因为它将占据整个屏幕高度。i、 e.每行项目都是屏幕高度。我想确保我了解您想要做什么。您想要一个a
RecyclerItem
这是其父级的完整大小RecyclerVIew
正确吗?使用android:id=“@+id/leftbar”
是否尝试将该项目从屏幕上删除?并且只有当用户滚动时才可见,或者您希望用户切换其可见性。如果您可以提供一个带有标签的所需图形,这会有所帮助。@VirtualProdigy否我只希望leftbar视图的高度与回收器视图集项目的高度相同android:layout\u alignParenTop=“true”
&android:layout\u alignParentBottom=“true”
。然后,leftbar
也将是其父容器的高度。为了使您的循环项成为父视图的高度,您很可能需要更新适配器。您必须获取“RecyclerView”的隐含高度,并在适配器的“onCreateViewHolder”中使用父级的高度设置RecyclerItem的LayoutParams
RelativeLayout.LayoutParams divParams = (RelativeLayout.LayoutParams)
divider.getLayoutParams();
divParams.height = height;//Add any top or bottom margin(in pixel) specified
divider.setLayoutParams(divParams);