理解LayoutInflater

1,包含的几个重要方法

public View inflate(int resource, ViewGroup root)

public View inflate(int resource, ViewGroup root, boolean attachToRoot)

inflater方法的作用就是将一个xml转换成View对象


2,我们经常用的adapter中的getView方法

public View getView( int position, View convertView, ViewGroup parent) {
     if (convertView == null ) {
         convertView = inflate(R.layout.item_row, null );
     }
     return convertView;
}
resource 布局的资源id ; root 填充的根视图 ;attachToRoot 是否将载入的视图绑定到根视图中

分析:上面的inflate方法中第二个参数如果为null的话,表示不指定root,将使用ViewGroup中默认创建的LayoutParams(宽高为wrap_content
,wrap_content),但是为什么我们经常使用的listView却可以指定null呢,因为listView所继承的AbsListView中的generateDefaultLayoutParams()
方法返回的是(宽高match_content,wrap_content)所以,我们item的根布局的android_xxx属性是(宽高match_content,wrap_content),但是recylerView
中如果指定为null,那么则根布局的android_xxx属性则是wrap_content,wrap_content。


来看代码:
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);
                         }
                     }
如果我们传入的第三个参数为flase,那么则使用我们自己的item的根布局的android_xxx宽高属性

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值