在使用View.Inflate(context,resid,root)创建recyclerview item布局时,根布局的margin,pading都没有起作用.

看了它其中的源码使用的是LayoutInflater.from().inflate(),层层跟进发现其中有一段逻辑:

   
public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) {.......省略代码                final View temp = createViewFromTag(root, name, inflaterContext, attrs);                ViewGroup.LayoutParams params = null;                if (root != null) {                    // 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);                    }                }                // We are supposed to attach all the views we found (int temp)                // to root. Do that now.                if (root != null && attachToRoot) {                    root.addView(temp, params);                }
.......省略代码        return result;    }}

也就是说,root!=null && attachToRoot==true,会将布局view添加到内部,作为它的父view,否则不会设置布局的layoutparams,但是如果我们在使用recyclerview创建item布局时并不需要添加到父view怎么办?


可以看到attachToRoot(是否附加到根布局)只需将这个参数传false,布局将不会添加到root中,并且还会设置布局的layoutparams。

调用:

LayoutInflater.from(context).inflate(R.layout.item_layout,parent,false);


更多相关文章

  1. 没有一行代码,「2020 新冠肺炎记忆」这个项目却登上了 GitHub 中
  2. android ImageView实现圆角(xml实现方法)
  3. RadioGroup组与onCheckedChanged事件
  4. listview中加入listbutton
  5. android中使用开源项目做出上拉、下拉刷新功能
  6. Android(安卓)keep screen on 方法
  7. Android源码编译开启ccache缓存
  8. 安卓EditText控件底部红线如何解决?
  9. 安卓学习笔记之自定义ContentProvider

随机推荐

  1. Android中保存并设置ListView位置
  2. Android异步处理一:使用Thread+Handler实
  3. Android的电话功能介绍
  4. Android控件(TextView、EditView、ImageVi
  5. 在android机器上安装GCC 编译环境
  6. Android(安卓)EditText常用属性功能汇总
  7. android官方下载链接最新版 (Windows版本
  8. Android中Message机制的灵活应用(一)
  9. Android(安卓)文件的上传
  10. Android(安卓)- 4种基本布局