您的位置:首页 > 移动开发 > Android开发

RecyclerView android:layout_width="match_parent" 无效

2016-01-30 12:32 465 查看
使用RecyclerView 时,在xml文件中设置宽度match_parent无效。

View view = mInflater.from(mContext).inflate(R.layout.item_recycler_view, parent, false);


对于:inflate

public View inflate(int resource, ViewGroup root, boolean attachToRoot) {
if (DEBUG) System.out.println("INFLATING from resource: " + resource);
XmlResourceParser parser = getContext().getResources().getLayout(resource);
try {
return inflate(parser, root, attachToRoot);
} finally {
parser.close();
}
}


即调用了:

public View inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot)


其方法对于理解后两个参数作用的代码:

ViewGroup.LayoutParams params = null;

if (root != null) {
if (DEBUG) {
System.out.println("Creating params from root: " +
root);
}
// Create layout params that match root, if supplied
params = root.generateLayoutParams(attrs);
if (!attachToRoot) {
// Set the layout params for temp if we are not
// attaching. (If we are, we use addView, below)
temp.setLayoutParams(params);
}
}


调用其方法:

1、传入了ViewGroup root参数,则会从root中得到由layout_width和layout_height组成的LayoutParams

2、如果attachToRoot设置为false的话,就会对我们加载的视图View设置该LayoutParams。

所以RecyclerView 如果采用平常方式去inflate,宽度不会显示全,设置layout_width="match_parent" 无效。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: