【问题标题】:RecyclerView wraps its conentRecyclerView 包装了它的内容
【发布时间】:2021-03-13 18:50:43
【问题描述】:

我正在使用 RecyclerView 为 Linear Vertical listViews 充气,但是,虽然子项的宽度设置为 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>

【问题讨论】:

  • 你在item_recyclerView 的第一个LinearLayout 上尝试过android:layout_weight="1" 吗?

标签: android android-fragments android-recyclerview android-linearlayout android-framelayout


【解决方案1】:

当扩展布局文件时,根视图上的 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 宽度被忽略的原因完全相同。

【讨论】:

    【解决方案2】:

    给你的父母线性布局 weightSum 5 给 imageView 重量 1 并做他的宽度 0 而对于子线性布局权重 4

    enter code here
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-26
      • 1970-01-01
      • 2010-12-31
      • 1970-01-01
      相关资源
      最近更新 更多