【问题标题】:match_parent width does not work in RecyclerViewmatch_parent 宽度在 RecyclerView 中不起作用
【发布时间】:2015-06-07 07:30:19
【问题描述】:

我的 RecyclerView 和项目有 match_parent 宽度,但结果是:

    <view
    class="android.support.v7.widget.RecyclerView"
    android:layout_width="match_parent"

和项目:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:fab="http://schemas.android.com/apk/res-auto"
android:id="@+id/ll_itm"
android:orientation="horizontal"
android:layout_width="match_parent"

满:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:fab="http://schemas.android.com/apk/res-auto"
android:id="@+id/ll_itm"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="100"
android:gravity="right"
>


<Button
    android:layout_width="0dp"
    android:layout_weight="15"
    android:layout_height="fill_parent"
    android:text="ملاحظات"
    android:id="@+id/button" />

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="20"
    android:gravity="center"
    >
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >

        <com.getbase.floatingactionbutton.FloatingActionButton
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            fab:fab_plusIconColor="#ff56ff83"
            fab:fab_colorNormal="@color/d_red"
            fab:fab_colorPressed="#ff5c86ff"
            fab:fab_size="mini"
            fab:fab_icon="@drawable/ic_remove_white"
            android:id="@+id/fab_rmv" />
        <esfandune.ir.elmikarbordiardakan.other.CustomTxtView
            android:layout_weight="25"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="0"
            android:gravity="right|center_vertical"
            android:id="@+id/txt_takhir_itm" />
        <com.getbase.floatingactionbutton.FloatingActionButton
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            fab:fab_plusIconColor="@color/colorprimarylight"
            fab:fab_colorNormal="@color/colorprimarydark"
            fab:fab_colorPressed="@color/colorprimary"
            fab:fab_size="mini"
            fab:fab_icon="@drawable/ic_add_white"
            android:id="@+id/fab_add" />

    </LinearLayout>
</LinearLayout>
    <Spinner
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="10"
        android:id="@+id/sp_nomre_itm"

        android:entries="@array/degrees"/>


<LinearLayout
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="10"
    android:gravity="center"
    >
    <!--LinearLayout baraye ine ke nameshod fab ro weight behosh dad-->
    <com.getbase.floatingactionbutton.FloatingActionButton
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        fab:fab_plusIconColor="#ff56ff83"
        fab:fab_colorNormal="@color/d_green"
        fab:fab_colorPressed="@color/d_orange"
        fab:fab_size="normal"
        fab:fab_icon="@drawable/ic_done_white"
        android:id="@+id/fab_hazr" />



</LinearLayout>
<esfandune.ir.elmikarbordiardakan.other.CustomTxtView
    android:layout_weight="5"
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="100"
    android:gravity="right|center_vertical"
    android:id="@+id/txt_ghybtNumber_itm" />

<esfandune.ir.elmikarbordiardakan.other.CustomTxtView
        android:layout_weight="30"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="عباسعلی ملاحسینی اردکانی"
        android:gravity="right|center_vertical"
        android:id="@+id/txt_title_itm"
    android:layout_marginRight="10dp"
    />

<view
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="10"
    class="de.hdodenhof.circleimageview.CircleImageView"
    android:id="@+id/view"
    android:src="@drawable/mmrdf"
   />
</LinearLayout>

【问题讨论】:

  • 确保你的 recyclerview 的 android:layout_width 属性应该有 match_parent 而不是 wrap_content

标签: android android-recyclerview


【解决方案1】:

在您为onCreateViewHolder 中的项目充气的适配器中,是inflate 调用null? 的第二个参数。

如果是这样,请将其更改为 parent,这是 onCreateViewHolder 函数签名中的第一个参数。

View rootView = LayoutInflater.from(context).inflate(R.layout.itemLayout, parent, false);

如果您需要第二个参数为null,那么当您获得关于inflating 的视图参考时,请执行以下操作

View rootView = LayoutInflater.from(context).inflate(R.layout.itemLayout, null, false);
RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
rootView.setLayoutParams(lp);
return new RecyclerViewHolder(rootView);

【讨论】:

  • 对我不起作用:如果我将项目的 rootView 设置为 width="match_parent" 但如果我将其设置为 500dp 则它“有效”。任何想法 ?好的,它不适用于 LinearLayout 但适用于项目上的 relativeLayout... wtf ?
  • 对不起,因为它适用于 relativeLayout,我只是将 LinearLayout 更改为 RelativeLayout。但是 RecyclerView 是 match_parent,并且 item rootView(所以是 LinearLayout)也是 Match_parent 并且它不起作用。我第一次遇到这种情况。
  • 哦,太好了,这行得通..拉我头发一个小时后。
  • 是的,如前所述,由于某种原因,这不适用于 LinearLayout,并且通过此修复将其切换到 RelativeLayout 似乎可以解决问题。
  • 这不适用于线性布局或约束布局。我不得不切换到 Relativelayout,然后它就起作用了
【解决方案2】:

在适配器的 onCreateViewHolder(...) 方法中,您必须将 ViewGroup 定义为父级。您将从 onCreateViewHolder(...) 方法的第一个参数中获得。

在我传递 ViewGroup 的第二个参数中查看下面的行。这将自动将视图与其父级匹配:

rowView=inflater.inflate(R.layout.home_custom_list, parent,false);

///完整代码如下

public View onCreateViewHolder(ViewGroup parent, int position) {
    // TODO Auto-generated method stub
    View rowView;
        LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        rowView=inflater.inflate(R.layout.home_custom_list, parent,false);

【讨论】:

    【解决方案3】:

    我使用FrameLayoutMATCH_PARENT 作为宽度,并看到与RecyclerView + LinearLayoutManager 相同的行为。在我在onCreateViewHolder 回调中执行以下操作之前,上述更改均不适用于我:

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // create a new view
        View v = LayoutInflater.from(parent.getContext())
                               .inflate(R.layout.note_layout, parent, false);
        v.setLayoutParams(new RecyclerView.LayoutParams(
              ((RecyclerView) parent).getLayoutManager().getWidth(),
              context.getResources()
                     .getDimensionPixelSize(R.dimen.note_item_height)));
    
        return new ViewHolder(v);
    }
    

    显然看起来像是(我猜)RecyclerView 实现中的一个错误。

    【讨论】:

    • 覆盖 layoutParams 是唯一对我有帮助的解决方案。任何人都知道这是否是 RecyclerView 中的错误?非常感谢您提供指向 Google 错误跟踪器的链接。
    【解决方案4】:

    当你在适配器中为你的项目设置布局参数时试试这个。

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

    【讨论】:

    • 什么是 ViewOffersHolder? :) 你的代码不完整
    【解决方案5】:

    我已经完成了这样的修复。在我的情况下,活动布局文件存在问题,因为我使用 ConstraintLayout 作为根活动布局。可能也是你的情况。

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <include
            android:id="@+id/toolBar"
            layout="@layout/toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    
        <FrameLayout
            android:id="@+id/fragment_container"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="@color/accent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@id/toolBar" />
    </android.support.constraint.ConstraintLayout>
    

    【讨论】:

    • 很难相信,但只是用 LinearLayout 替换我周围的 ConstraintLayout 也为我解决了这个问题......
    • match_parent 不应用于 ConstraintLayout 的直接子级。相反,使用 0dp 和 constraintLeft / Right / Bottom / Top 属性。
    【解决方案6】:

    就我而言,问题出在RecyclerView XML 声明中,layout_width 是 0dp,这意味着 match_constraints,当我将其更改为 match_parent 时,项目开始填充所有 RecyclerView 宽度:

    <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recyclerView"
                android:layout_width="0dp"  <-- changed this to "match_parent"
                android:layout_height="0dp"
                android:layout_marginBottom="45dp"
                android:background="@android:color/transparent"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintHeight_default="wrap"
                app:layout_constraintHeight_max="360dp"
                app:layout_constraintHeight_min="60dp"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@id/header"/>
    

    【讨论】:

      【解决方案7】:

      这对我有用。

      替换这个

         View view = View.inflate(parent.getContext(), R.layout.row_timeline, null);
         return new TimeLineViewHolder(view, viewType);
      

      通过这个

       View rootView = LayoutInflater.from(context).inflate(R.layout.row_timeline, null, false);
              RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
              rootView.setLayoutParams(lp);
              return new TimeLineViewHolder(rootView, viewType);
      

      【讨论】:

        【解决方案8】:

        只需在根目录中添加一个高度为 0 且全宽的虚拟视图对我有用。

        <androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                tools:context=".screen.gallery.GalleryFragment">
        
                <!----DUMMY VIEW----->
                <View
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    />
        
              
                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/navigationList"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                  />
        

        【讨论】:

        • 检查 OP 正在寻找什么,然后发布您的答案!问题不在于recyclerview。它是它的项目!
        • 谢谢!我确实讨厌这是我可以开始工作的唯一解决方案。就我而言,这是一个约束布局,我必须将虚拟视图放入其中。
        【解决方案9】:

        我解决了这个问题:

         myInflatedRowLayout.getLayoutParams().width = vg.getWidth();
        

        它将MATCH_PARENT 替换为RecyclerView 的实际宽度。

        【讨论】:

          【解决方案10】:

          当我在回收器视图中使用的复合视图使用线性布局和约束布局时,我遇到了同样的问题,但是当我更改为这些复合视图的相对布局时,它就起作用了。

          【讨论】:

          • 删除任何未回答的内容后,这似乎没有提供任何额外的见解。如果您不能 edit 对此并根据 How to Answer 请删除此。
          • 对不起,如果不够清楚,但让这个答案留下来,我浪费了很多时间来寻找这个问题的解决方案,这可能会对将来的人有所帮助。
          • 您提到的唯一解决方案细节是“相对布局”。这已经在其他答案中提到过。抱歉,花了你这么长时间,但仅提及此处并没有提供任何新内容。如果您可以详细了解您的解决方案并确保它实际上是该铺路顶部问题中描述的问题的解决方案,那将会很好。照原样,这篇文章没有添加任何内容。请edit提高这个的帮助。
          • 这个解决方案在我的情况下工作正常
          【解决方案11】:

          看不到您的完整代码,但可以猜到,您的 LinearLayout 中的某些视图是“wrap_content”。您需要使用 'android:layout_weight="1"' 使其中一个或一些扩展至全宽

          更新: 你有很多多余的layout_weight。将它们全部设为“wrap_content”,并且只为其中一个添加layout_weight=1 - 用于最后一个CustomTextView。这样一来,它就会占据所有的空白空间。

          【讨论】:

          • 体重可能不是原因。但公平地说,它被声明为 match_parent 正在被使用。
          【解决方案12】:

          我已经被这个问题困扰了一段时间,对我有用的解决方案是放置 2 个带有 match_parent 的视图,一个在另一个里面。

          如果我在列表项的布局上这样做:

          <?xml version="1.0" encoding="utf-8"?>
          <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:background="#F00"
              xmlns:app="http://schemas.android.com/apk/res-auto"
              android:layout_height="match_parent">
          
          </RelativeLayout>
          

          虽然它是其他人提到的相对布局,并且我正在传递父视图但在充气器中是错误的,但它根本不会显示(检查红色背景)。

          但是,当我这样做时:

          <?xml version="1.0" encoding="utf-8"?>
          <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              xmlns:app="http://schemas.android.com/apk/res-auto"
              android:layout_height="match_parent">
          
              <androidx.constraintlayout.widget.ConstraintLayout
                  android:layout_width="match_parent"
                  android:background="#0F0"
                  android:layout_height="match_parent"/>
          
          </RelativeLayout>
          

          绿色的布局出现了,占据了整个空间。

          所以在主视图中使用 match_parent 就可以解决问题,不知道为什么。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2014-08-21
            • 1970-01-01
            • 2016-06-24
            • 1970-01-01
            • 1970-01-01
            • 2020-09-23
            • 1970-01-01
            相关资源
            最近更新 更多