Android RecyclerView包装了它的内容
我正在使用RecyclerView膨胀线性垂直列表视图,但是,尽管子视图的宽度设置为与父视图匹配,但RecyclerView在运行时会将其包装起来 以下是问题的屏幕截图: 项目\u回收视图Android RecyclerView包装了它的内容,android,android-fragments,android-recyclerview,android-linearlayout,android-framelayout,Android,Android Fragments,Android Recyclerview,Android Linearlayout,Android Framelayout,我正在使用RecyclerView膨胀线性垂直列表视图,但是,尽管子视图的宽度设置为与父视图匹配,但RecyclerView在运行时会将其包装起来 以下是问题的屏幕截图: 项目\u回收视图 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/tag_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="0dp"
android:maxLines="2"/>
<TextView
android:id="@+id/tag_role"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<com.google.android.material.imageview.ShapeableImageView
android:id="@+id/user_pic"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_weight="0"
android:rotation="-45"
android:scaleType="centerCrop"
android:src="@drawable/ic_user"
app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay.App.circleImageView" />
</LinearLayout>
对布局文件进行充气时,根视图上的
布局属性将被忽略,除非在充气()调用中指定父视图组。您当前没有这样做
在onCreateViewHolder()
方法中,替换以下内容:
为此:
ItemQuestionPostBinding binding = ItemQuestionPostBinding.inflate(inflater, parent, false);
另外请注意,您可能不想使用match\u parent
作为您的RecyclerView项目的高度:
这将使您的RecyclerView中只有一个项目可见,因为每个项目都是屏幕的整个高度。这目前不是一个问题,原因与忽略父项宽度的原因完全相同。为父项线性设置权重总和5
为imageView提供权重1,并将其宽度设置为0
对于儿童直系亲属,体重为4
enter code here
您是否在项目回收视图的第一个线性布局上尝试过android:layout\u weight=“1”
?
android:layout_width="match_parent"
android:layout_height="match_parent"
enter code here