本文介绍了CardView layout_width =" match_parent"不匹配父RecyclerView宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个片段,包含RecyclerView与layout_width =match_parent:

I have a fragment with contains a RecyclerView with layout_width="match_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:

The item in the RecyclerView is a CardView also with 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>

我夸大如下项目视图:

I inflate the item view as below:

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如下图所示:

But when I run the app, the CardView is rendered as wrap_content as shown below:

请注意,这是运行在模拟器,而不是一个真正的设备。

Note that this was run on emulator, not a real device.

难道我做错了什么,或者是一个错误?

Am I doing something wrong, or is it a bug?

推荐答案

该文档的膨胀

充气从指定的XML资源的新视图层次。引发  InflateException如果存在一个错误。

参数
  资源 ID为一个XML布局资源加载(如:  R.layout.main_page)根
  查看是的父  生成的层次结构(如attachToRoot是真实的),或者只是一个  对象,它提供了一组的LayoutParams的值对的根  返回层次结构(如果attachToRoot是假的。)
   attachToRoot 无论是  膨胀的层次应附加到根参数?如果  假的,根仅用于创建正确的子类  的LayoutParams用于在XML根视图。返回的根视图  膨胀的层次结构。如果根被提供,attachToRoot是真实的,  这是根;否则它是膨胀的XML文件的根

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.

在这里是非常重要的的没有的供应真实的,但的的供应父:

It is important here to not supply true, but do supply the parent:

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

直供 View允许充气知道的LayoutParams使用。直供参数告诉它的没有的将其连接到父,只是还没有。这正是 RecyclerView 会为你做。

Supplying the parent View lets the inflater know what layoutparams to use. Supplying the false parameter tells it to not attach it to the parent just yet. That is what the RecyclerView will do for you.

这篇关于CardView layout_width =&QUOT; match_parent&QUOT;不匹配父RecyclerView宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 14:31
查看更多