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

Pro*_*mar 118 android android-layout android-cardview android-5.0-lollipop android-recyclerview

我有一个包含RecyclerView的片段,其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" />
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

我将物品视图膨胀如下:

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;
    }
Run Code Online (Sandbox Code Playgroud)

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

CardsBug

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

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

nha*_*man 282

文档inflate:

从指定的xml资源中扩充新的视图层次结构.如果出现错误,则抛出InflateException.

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

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

LayoutInflater.from(parent.getContext())
            .inflate(R.layout.card_listitem, parent, false);
Run Code Online (Sandbox Code Playgroud)

提供parent视图让inflater知道要使用哪些layoutparams.提供false参数告诉它不要将它附加到父级.这就是你的RecyclerView意志.

  • 该解决方案对我不起作用,因为ListView的`layout_width`是`wrap_content`.它很奇怪,因为这不应该影响列表视图!所以我的理解是,当你提供false作为layoutinflater的参数时,ListView将列表项添加到ListView,其布局参数与ListView本身相匹配!:0 (4认同)
  • 我认为正确的理解是,当您将attachToRoot设置为false时,您需要提供具有已定义的LayoutParams值的对象.该对象不一定是父对象."......只是一个为返回层次结构的根提供一组LayoutParams值的对象(如果attachToRoot为false.)......"但我不明白为什么inflater无法读取CardView中定义但需要的LayoutParams通知它的另一个对象参数? (3认同)

Gan*_*nna 59

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

<RelativeLayout 
    android:layout_width="match_parent" ... >
    <CardView 
        android:layout_width="match_parent" ... >
    </CardView>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

  • 它确实有效.什么是解释? (2认同)
  • 已经过去很多年了,android x 已经投入生产了,但 bug 仍然没有修复! (2认同)
  • 最简单的方法。 (2认同)

anu*_*ake 12

这对我有用,

 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);
Run Code Online (Sandbox Code Playgroud)


Rah*_*hul 6

我这样做的方式是:

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));
Run Code Online (Sandbox Code Playgroud)

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

  • 请稍微改进一下。它基本上是说“试试这个” (2认同)

spe*_*ads 5

这似乎是固定的 'com.android.support:recyclerview-v7:23.2.1'

请参阅:RecyclerView wrap_content以进行进一步讨论.