在RecyclerView元素周围给出边框的正确方法

时间:2016-11-19 07:20:31

标签: android android-recyclerview

我有RecyclerView纵向LinearLayoutManager。我必须围绕此recyclerView的元素设置边框。

我的方法是创建drawable rectangle形状,其stroke具有边框颜色,并将此drawable设置为{{1}行的背景}}

这是我的背景:

recyclerView

这种方法的问题在于行的内部边界(将其显示为垂直列表的分隔线)由于矩形背景的底部和顶部的重叠而被绘制为所需宽度的两倍,而我想要所有边框宽度相同。

所以,我尝试在行<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <stroke android:width="1dp" android:color="#979797" /> <solid android:color="#424447" /> </shape> 的四边放置4个视图,并考虑根据它的位置操纵layout个边框。这是我的行布局:

visibility

问题是左边框和右边框没有出现。我猜这些观点无法计算出它们的高度。我以为我会计算行的高度并手动给出左右边框这个高度。因此,我尝试在<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="wrap_content"> <!--android:background="@drawable/background_collection">--> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="12dp"> <ImageView android:id="@+id/collectionThumb" android:layout_width="44dp" android:layout_height="44dp" android:layout_centerVertical="true" android:layout_marginEnd="12dp" android:layout_marginRight="12dp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toEndOf="@+id/collectionThumb" android:layout_toLeftOf="@+id/collectButton" android:layout_toRightOf="@+id/collectionThumb" android:layout_toStartOf="@+id/collectButton" android:orientation="vertical"> <TextView android:id="@+id/collectionName" android:layout_width="match_parent" android:layout_height="wrap_content" android:maxLines="1" android:textColor="#F0F0E9" android:textSize="14sp" /> <TextView android:id="@+id/subText" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="#8a8a8a" android:textSize="8dp" /> </LinearLayout> <ImageView android:id="@+id/collectButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentEnd="true" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginLeft="12dp" android:layout_marginStart="12dp" android:src="@drawable/ic_collect_default" /> </RelativeLayout> <View android:id="@+id/topBorder" android:layout_width="match_parent" android:layout_height="1dp" android:layout_alignParentTop="true" android:background="#979797" /> <View android:id="@+id/bottomBorder" android:layout_width="match_parent" android:layout_height="1dp" android:layout_alignParentBottom="true" android:background="#979797" /> <View android:id="@+id/leftBorder" android:layout_width="1dp" android:layout_height="match_parent" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:background="#979797" /> <View android:id="@+id/rightBorder" android:layout_width="1dp" android:layout_height="match_parent" android:layout_alignParentEnd="true" android:layout_alignParentRight="true" android:background="#979797" /> <ImageView android:id="@+id/triangleTip" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_marginLeft="20.9dp" android:layout_marginStart="20.9dp" android:src="@drawable/shape_top_arrow_fancy" android:visibility="invisible" /> </RelativeLayout> height的{​​{1}}中找到ViewGroup的{​​{1}} id container onBindViewHolder()。我尝试使用recyclerView'sadapter甚至使用getHeight(),但我没有得到正确的getMeasuredHeight()。使用LayoutParams,我在其他情况下获得height LayoutParamsheight

我该如何解决这个问题?我的方法是否正确?有更好的方法吗?

0 个答案:

没有答案