我有一个片段,其中包含一个布局为\u width="match\u parent"的回收视图:

<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity$PlaceholderFragment" />

RecyclerView中的项目是一个CardView,也带有layout_width="match_parent":

<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_gravity="center"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="20dp"
    card_view:cardCornerRadius="4dp">

    <TextView
        android:layout_gravity="center"
        android:id="@+id/info_text"
        android:layout_width="match_parent"
        android:gravity="center"
        android:layout_height="match_parent"
        android:textAppearance="?android:textAppearanceLarge"/>
</android.support.v7.widget.CardView>

我将项目视图放大如下:

public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                   int viewType) {
        CardView v = (CardView) LayoutInflater.from(parent.getContext())
                .inflate(R.layout.card_listitem, null, true);

        ViewHolder vh = new ViewHolder(v);
        return vh;
    }

但是当我运行应用程序时,CardView呈现为WRAP_CONTENT,如下所示:

CardsBug

请注意,这是在模拟器上运行的,而不是在真实设备上运行.

我是做错了什么,还是一只虫子?

推荐答案

inflate人的文件:

从指定的xml资源展开新的视图层次 struct .投掷

Parameters
resource ID for an XML layout resource to load (e.g., R.layout.main_page) root
view to be the parent of the generated hierarchy (if attachToRoot is true), or else simply an object that provides a set of LayoutParams values for root of the returned hierarchy (if attachToRoot is false.)
attachToRoot Whether the inflated hierarchy should be attached to the root parameter? If false, root is only used to create the correct subclass of LayoutParams for the root view in the XML. Returns The root View of the inflated hierarchy. If root was supplied and attachToRoot is true, this is root; otherwise it is the root of the inflated XML file.

在这里,重要的是not提供真实的信息,但do提供给父母:

LayoutInflater.from(parent.getContext())
            .inflate(R.layout.card_listitem, parent, false);

提供parent视图可以让充气机知道要使用的布局参数.提供false参数会告诉not将其附加到父级.这就是RecyclerView将为你做的.

Android相关问答推荐

Android使用参数编写齐射后请求

在Android Studio Iguana 2023.2.1中,哪里可以找到能量分析器?

如何使TextField的背景透明?

房间DB:UPSERT返回什么?

处理Room数据库中的嵌套实体

Yarn 机器人导致活动未找到,但Gradlew Run工作正常

两首合成曲的喷气背包动画

FireBase Android ChildEventListener在被规则拒绝时触发(RTDB)

Android Kotlin - 计费 - 从应用内购买获取productId

使用 async 向网络发出并行请求并在supervisorScope中处理它们

Gradle在我的Android Compose项目中继续推广依赖版本

当 Firebase Firestore 发生变化时,Kotlin ViewModel 不会更新

为一组闪烁的可组合项制作动画,控制同步/定时

Android:appcompat 和 material 如何从默认创建 appcompat 和 material 视图?

可组合的可见性不随状态变化而变化

如何将文本组合放在行中,一个具有可变宽度的组合

Jetpack 使用 Canvas 组成半圆

在jetpack compose中看不到圆角

Jetpack Compose 中的按钮上的文本未更新

如何使伴奏导航 BottomSheet 完全展开?