Android CardView layout_width="match_parent" 与父 RecyclerView 宽度不匹配

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24503760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:18:11  来源:igfitidea点击:

CardView layout_width="match_parent" does not match parent RecyclerView width

androidandroid-layoutandroid-5.0-lollipopandroid-recyclerviewandroid-cardview

提问by ProgrAmmar

I have a fragment with contains a RecyclerView with layout_width="match_parent":

我有一个片段,其中包含一个带有 layout_width="match_parent" 的 RecyclerView:

<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" />

The item in the RecyclerView is a CardView also with layout_width="match_parent":

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>

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;
    }

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

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

CardsBug

卡虫

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

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

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

我做错了什么,还是一个错误?

回答by nhaarman

The docs for inflate:

的文档inflate

Inflate a new view hierarchy from the specified xml resource. Throws InflateException if there is an error.

Parameters
resourceID for an XML layout resource to load (e.g., R.layout.main_page) root
viewto 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.)
attachToRootWhether 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.

从指定的 xml 资源膨胀一个新的视图层次结构。如果有错误,则抛出 InflateException。

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

It is important here to notsupply true, but dosupply the parent:

到这里重要的不是提供真实的,但提供父:

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

Supplying the parentView lets the inflater know what layoutparams to use. Supplying the falseparameter tells it to notattach it to the parent just yet. That is what the RecyclerViewwill do for you.

提供parent视图让充气器知道要使用什么布局参数。提供false参数告诉它不要将它附加到父级。这就是RecyclerView意志为你做的事情。

回答by Ganesh Kanna

Use RelativeLayout as the immediate parent to CardView.

使用 RelativeLayout 作为 CardView 的直接父级。

<RelativeLayout 
    android:layout_width="match_parent" ... >
    <CardView 
        android:layout_width="match_parent" ... >
    </CardView>
</RelativeLayout>

回答by anurag_dake

This worked for me,

这对我有用,

 View viewHolder= LayoutInflater.from(parent.getContext())
            .inflate(R.layout.item, null, false);
 viewHolder.setLayoutParams(new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.WRAP_CONTENT));
 return new ViewOffersHolder(viewHolder);

回答by Rahul

The way I did it is:

我这样做的方式是:

View view = mInflater.inflate(R.layout.row_cardview, null, true);
            WindowManager windowManager = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
            int width = windowManager.getDefaultDisplay().getWidth();
            view.setLayoutParams(new RecyclerView.LayoutParams(width, RecyclerView.LayoutParams.MATCH_PARENT));

Explanation:In your onCreateViewHolde once you get card view, get the screen width and then set the layout param accordingly for card view.

说明:在您获得卡片视图后,在 onCreateViewHolde 中,获取屏幕宽度,然后相应地为卡片视图设置布局参数。

回答by speedynomads

This appears to be fixed in 'com.android.support:recyclerview-v7:23.2.1'

这似乎已修复 'com.android.support:recyclerview-v7:23.2.1'

See: RecyclerView wrap_contentfor further discussions.

有关进一步讨论,请参阅:RecyclerView wrap_content

回答by issamux

Default implementation force the use of wrap_contentin the default layout manager :

默认实现强制在默认布局管理器中使用wrap_content

  @Override
public LayoutParams generateDefaultLayoutParams() {
    return new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT);
}

All you have to do is to override this method in your LayoutManager like this :

你所要做的就是在你的 LayoutManager 中像这样覆盖这个方法:

  @Override
public LayoutParams generateDefaultLayoutParams() {
    return new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT);
}

回答by Vlad

Simply set new layout params

只需设置新的布局参数

view.setLayoutParams(new RecyclerView.LayoutParams(
    RecyclerView.LayoutParams.MATCH_PARENT,
    RecyclerView.LayoutParams.WRAP_CONTENT
));

回答by DiRiNoiD

This works for me

这对我有用

View view = inflator.inflate(R.layout.layout_item, ***null***, false);

回答by Vladimir Salguero

Use card_view:contentPadding with a negative value

使用带有负值的 card_view:contentPadding

<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:layout_width="match_parent"
    card_view:contentPadding="-4dp"
    card_view:cardElevation="0dp"
    android:layout_height="wrap_content">

It worked for me

它对我有用

回答by Samuel Moshie

Wrap CardViewin a Layout that has width:match_parent

包装CardView在具有宽度的布局中:match_parent