我正在使用 RecyclerView 来膨胀线性垂直 ListView ,但是,尽 pipe View 的宽度设置为 match_parent,但 RecyclerView 在运行时会对其进行包装。
这是问题的屏幕截图:
item_recyclerView
<?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>
最佳答案
膨胀布局文件时, Root View 上的 layout_
属性将被忽略,除非您在 inflate()
中指定父 ViewGroup
> 打电话。您目前没有这样做。
在您的 onCreateViewHolder()
方法中,替换以下内容:
ItemQuestionPostBinding binding = ItemQuestionPostBinding.inflate(inflater);
这样:
ItemQuestionPostBinding binding = ItemQuestionPostBinding.inflate(inflater, parent, false);
另请注意,您可能不希望使用 match_parent
作为 RecyclerView 项目的高度:
android:layout_width="match_parent" android:layout_height="match_parent"
这将使 RecyclerView 中只有一个项目可见,因为每个项目都是屏幕的整个高度。目前这不是问题,原因与忽略 match_parent
宽度完全相同。
关于android - RecyclerView 包装其内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66617338/