额,好长时间没有写博客了,本来预计的计划是每个月写一篇博客的。算了,废话不多说,今天遇到RecyclerView的item没有充满的整个宽度,这里记录一下原因.
1.第一种是在adapter中加载inflater时parent传入为null
item的布局是这样的:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="160dp"
android:background="#48308e"
android:orientation="vertical"
android:padding="10dp">
android:background="@color/line_background_e7e7e7">
<TextView
android:id="@+id/lauch_break_txt"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:gravity="center"
android:textColor="#bf2780"
android:textSize="18sp"/>
</LinearLayout>
可是,在实际效果中,item并没有充满,match_parent完全没有效果,效果是wrap_content自适应
后面仔细看了代码,发现是在适配器中加载布局时候的问题
一开始是这样写的,在onCreateViewHolder()中
view = View.inflate(mContext, R.layout.item,null); `
return new ClassDayViewHolder(view);
后面将这代码改成
view = LayoutInflater.from(mContext).inflate(R.layout.item_today_course,parent,false);
就没有出现item显示不全的问题了。
2.如果第一种不行,第二种原因就是在RecyclerView外随意加一层布局即可充满
原因是在写Recycleview布局时候未明确宽高,用权重代替。在RecyclerView外用LinearLayout或者RelativeLayout包裹嵌套。今天就记录到这里了。
转载自:https://blog.csdn.net/u014727709/article/details/84973908
欢迎start,欢迎评论,欢迎指正