match_parent 宽度在 RecyclerView 中不起作用 [英] match_parent width does not work in RecyclerView
本文介绍了match_parent 宽度在 RecyclerView 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的 RecyclerView 和 item 具有 match_parent 宽度,但结果是:
My RecyclerView and item has match_parent width but the result is :
<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>
推荐答案
在你的适配器中,你在 onCreateViewHolder
中膨胀项目,是 inflate
的第二个参数调用 null
?.
In your adapter where you are inflating the item in onCreateViewHolder
, is the second parameter of the inflate
call null
?.
如果是这样,请将其更改为 parent
,这是 onCreateViewHolder
函数签名中的第一个参数.
If so change it to parent
which is the first parameter in the onCreateViewHolder
function signature.
View rootView = LayoutInflater.from(context).inflate(R.layout.itemLayout, parent, false);
如果您需要第二个参数为 null
那么当您获得有关膨胀的视图引用时,请执行以下操作
If you need the second parameter to be null
then when you get the view reference on inflating, do the following
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);
这篇关于match_parent 宽度在 RecyclerView 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文