【问题标题】:RecyclerView Squeezes Child items widthRecyclerView 挤压子项宽度
【发布时间】:2021-04-24 11:33:22
【问题描述】:

我将 RecyclerView 与 Horizo​​ntal LinearLayoutManager 一起使用。 RecyclerView 的 child 是 CardView,虽然 child 的宽度参数仍然设置,但 recycler view 将其膨胀为挤压宽度

这是我的 RecyclerView

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv_popular_courses"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:listitem="@layout/card_popular_quizzes" />

这里是 card_popular_quizzes.xml

<androidx.cardview.widget.CardView
    android:layout_width="150dp"
    android:layout_height="wrap_content"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:minHeight="150dp"
    app:cardElevation="@dimen/card_itemcourse_elevation"
    app:cardBackgroundColor="#309C98"
    app:cardCornerRadius="16dp"
    >

<!--    app:cardElevation="10dp"-->

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_gravity="center">

        <ImageView
            android:id="@+id/quiz_thumbnail_imgvw"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:adjustViewBounds="true"
            android:cropToPadding="true"
            android:maxWidth="100dp"
            android:src="@drawable/android_developer" />

        <TextView
            android:id="@+id/quiz_textvw"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Hall of Fame"
            android:textColor="@color/whiteTextColor"
            android:textSize="16sp"
            android:textStyle="bold"
            android:layout_marginBottom="10dp"/>

    </LinearLayout>


</androidx.cardview.widget.CardView>

另外,recyclerview 在片段内,这是我添加 LinearLayoutManager 的代码

    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //removed some view initialisations

        RecyclerView rvPopularCourses=view.findViewById(R.id.rv_popular_courses);
        rvPopularCourses.setLayoutManager(
                new LinearLayoutManager(view.getContext(),LinearLayoutManager.HORIZONTAL,false));
        rvPopularCourses.setAdapter(quizAdapter);
    }

这是一个视觉截图供参考

【问题讨论】:

标签: android android-layout android-fragments android-recyclerview android-cardview


【解决方案1】:

LinearLayout 宽度是导致此问题的 wrap_content。 您必须在 card_popular_quizzes.xml 中将您的 LinearLayout layout_width 设置为 match_parent

【讨论】:

    猜你喜欢
    • 2016-04-26
    • 1970-01-01
    • 1970-01-01
    • 2013-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多