ListView item布局layout_width和layout_height属性

  1. 首先了解些关于布局的知识:
    LayoutParams类:LayoutParams are used by views to tell their parents how they want to be laid out,是告诉父布局它想如何展示;其属性match_parent, warp_content都是相对于父布局来说的,父布局在 measure 子布局的时候首先会获取子布局的LayoutParams: LayoutParams p = (LayoutParams) child.getLayoutParams();之后结合父布局的widthMeasureSpecheightMeasureSpec得到childWidthSpec, childHeightSpec之后调用child.measure(childWidthSpec, childHeightSpec);对child进行measure;

  2. ListView通过getView函数获得item view,具体是通过函数:public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot);的调用来生成view;

  3. inflate函数有三种调用方式:

    1.view = inflater.inflate(R.layout.view_item, viewGroup, false);
    2.view = inflater.inflate(R.layout.view_item, null);
    3.view = inflater.inflate(R.layout.view_item, viewGroup, false);

    对于listview支持两种1和2;
    对于方式3是不支持的,如果使用这种调用方式,程序会出错,在inflate执行到下面的addview时出错:

    if (root != null && attachToRoot) {
        root.addView(temp, params);
    }

    root就是ListView也就是AdapterView,很遗憾不支持addview;

    throw new UnsupportedOperationException("addView(View, LayoutParams) "
                + "is not supported in AdapterView");

    所以只能使用方式1和2;
    这里的讲解可以参考这里

  4. 使用方式1,那么item的顶层布局的layout_widthlayout_height都将失效,请无视它们吧,运行时啥作用也不会起;原因看下面代码

    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);
    }
    
    // Decide whether to return the root that was passed in or the
    // top view found in xml.
    if (root == null || !attachToRoot) {
        result = temp;
    }
    ......
    return result;

    如上final View temp = createViewFromTag(root, name, inflaterContext, attrs);这里新鲜 出炉的view的layoutParams是null;而如果root == null,则temp.setLayoutParams(params);不 会执行,result = temp;之后就返回了,listview在measure的时候对LayoutParams为null这种情况作 了判断:
    ListView的layout_heightwarp_content时:

    private void measureScrapChild(View child, int position, int widthMeasureSpec) {
        LayoutParams p = (LayoutParams) child.getLayoutParams();
        if (p == null) {
            p = (AbsListView.LayoutParams) generateDefaultLayoutParams();
            child.setLayoutParams(p);
        }

    ListView的layout_heightmatch_parent时,执行layout()时进行了判断:

     private void setupChild(View child, int position, int y, boolean flowDown, int childrenLeft,
                            boolean selected, boolean recycled) {
        ......
        AbsListView.LayoutParams p = (AbsListView.LayoutParams) child.getLayoutParams();
        if (p == null) {
            p = (AbsListView.LayoutParams) generateDefaultLayoutParams();
        }
    }

    再来看下generateDefaultLayoutParams函数:

    @Override
    protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
        return new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT, 0);
    }

    ViewGroup.LayoutParams.WRAP_CONTENT就不用看了,很多时候没啥用,这也是root为null时我们的 item布局总是能横向铺满的原因了;

  5. root不为null的情况,则执行params = root.generateLayoutParams(attrs);这里就获得了当初设置的layout_widthlayout_height;之后执行了temp.setLayoutParams(params);用于addview以及addViewInner;所以item顶层width或height的具体数值,如:100dp是有作用的;
  6. 很遗憾,item的布局顶层以及之下的match_parent或者warp_content都没啥作用,因为ListView的childHeightSpec 完全是由子view的LayoutParams决定的;除非子view自己争气挣点高度空间,否则高度就是0,比如放个view,高度match_parent,布局预览的时候好好的,运行的时候就消失了,因为高度是0;例外是父层布局有具体的高度,则其子view的match_parentwarp_content仍然有意义;想一想也是这个道理,ListView本来的高度就是不固定,你match_parent,想match到哪里去;照例找下代码的证据:

    private void setupChild(View child, int position, int y, boolean flowDown, int childrenLeft,
                            boolean selected, boolean recycled) {
        ......
        AbsListView.LayoutParams p = (AbsListView.LayoutParams) child.getLayoutParams();
        ......
        int lpHeight = p.height;
        int childHeightSpec;
        if (lpHeight > 0) {
            childHeightSpec = MeasureSpec.makeMeasureSpec(lpHeight, MeasureSpec.EXACTLY);
        } else {
            childHeightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
        }
        child.measure(childWidthSpec, childHeightSpec);
    }
    private void measureScrapChild(View child, int position, int widthMeasureSpec) {
        LayoutParams p = (LayoutParams) child.getLayoutParams();
        ......
        int lpHeight = p.height;
        ......
        if (lpHeight > 0) {
            childHeightSpec = MeasureSpec.makeMeasureSpec(lpHeight, MeasureSpec.EXACTLY);
        } else {
            childHeightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
        }
        child.measure(childWidthSpec, childHeightSpec);
    }

    MATCH_PARENT值是-1WRAP_CONTENT值是-2,所以lpHeight < 0将执行childHeightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);对于子view来说,就是看它自己的本事,自己有多大占多大;MeasureSpec.UNSPECIFIED可以传递,子view如果还是match_parent或者warp_content,那还会执行childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);

对于view的绘制流程讲解,推荐看这里

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值