Android layout_Android源码

Android layout_Android源码LayoutParams源码分析LayoutParams是布局参数的意思,我们在XML布局文件里的layout_xxx等属性都是对LayoutParams的描述。LayoutParams不属于View,是ViewGroup控制View的具体显示在哪里。

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

LayoutParams源码分析

概述

  • LayoutParams是布局参数的意思,我们在XML布局文件里的layout_xxx等属性都是对LayoutParams的描述。
  • LayoutParams不属于View,是ViewGroup控制View的具体显示在哪里。

LayoutParams基本用法

TextView textView1 = new TextView(this);
textView1.setText("TextView1");
linearLayout.addView(textView1);

TextView textView2 = new TextView(this);
textView2.setText("TextView2");
textView2.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
linearLayout.addView(textView2);

TextView textView3 = new TextView(this);
textView3.setText("TextView3");
textView3.setLayoutParams(new LinearLayout.LayoutParams(100, 100));
linearLayout.addView(textView3);

LayoutParams源码分析

LayoutParams继承关系

ViewGroup.LayoutParams

public LayoutParams(Context c, AttributeSet attrs) { 
   
    TypedArray a = c.obtainStyledAttributes(attrs, R.styleable.ViewGroup_Layout);
    setBaseAttributes(a,
                      R.styleable.ViewGroup_Layout_layout_width,
                      R.styleable.ViewGroup_Layout_layout_height);
    a.recycle();
}

public LayoutParams(int width, int height) { 
   
    this.width = width;
    this.height = height;
}

public LayoutParams(LayoutParams source) { 
   
    this.width = source.width;
    this.height = source.height;
}

LayoutParams() { 
   
}

ViewGroup.MarginLayoutParams

public static class MarginLayoutParams extends ViewGroup.LayoutParams { 
   
    public int leftMargin;

    public int topMargin;

    public int rightMargin;

    public int bottomMargin;

    private int startMargin = DEFAULT_MARGIN_RELATIVE;

    private int endMargin = DEFAULT_MARGIN_RELATIVE;

    public static final int DEFAULT_MARGIN_RELATIVE = Integer.MIN_VALUE;

    public MarginLayoutParams(Context c, AttributeSet attrs) { 
   
        super();

        TypedArray a = c.obtainStyledAttributes(attrs, R.styleable.ViewGroup_MarginLayout);
        setBaseAttributes(a,
                          R.styleable.ViewGroup_MarginLayout_layout_width,
                          R.styleable.ViewGroup_MarginLayout_layout_height);

        int margin = a.getDimensionPixelSize(
            com.android.internal.R.styleable.ViewGroup_MarginLayout_layout_margin, -1);
        if (margin >= 0) { 
   
            leftMargin = margin;
            topMargin = margin;
            rightMargin= margin;
            bottomMargin = margin;
        } else { 
   
            int horizontalMargin = a.getDimensionPixelSize(
                R.styleable.ViewGroup_MarginLayout_layout_marginHorizontal, -1);
            int verticalMargin = a.getDimensionPixelSize(
                R.styleable.ViewGroup_MarginLayout_layout_marginVertical, -1);
            ...
        }
        ...
    }
}

addView流程

public void addView(View child) { 
   
    addView(child, -1);
}

public void addView(View child, int index) { 
   
    if (child == null) { 
   
        throw new IllegalArgumentException("Cannot add a null child view to a ViewGroup");
    }
    LayoutParams params = child.getLayoutParams();

    //若子View的LayoutParams为null,则使用默认LayoutParams
    if (params == null) { 
   
        params = generateDefaultLayoutParams();
        if (params == null) { 
   
            throw new IllegalArgumentException(
                "generateDefaultLayoutParams() cannot return null");
        }
    }
    addView(child, index, params);
}

//生成默认LayoutParams
@Override
protected LayoutParams generateDefaultLayoutParams() { 
   
    if (mOrientation == HORIZONTAL) { 
   
        return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    } else if (mOrientation == VERTICAL) { 
   
        return new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    }
    return null;
}

public void addView(View child, int index, LayoutParams params) { 
   
    if (DBG) { 
   
        System.out.println(this + " addView");
    }

    if (child == null) { 
   
        throw new IllegalArgumentException("Cannot add a null child view to a ViewGroup");
    }

    requestLayout();
    invalidate(true);
    addViewInner(child, index, params, false);
}

private void addViewInner(View child, int index, LayoutParams params,
                          boolean preventRequestLayout) { 
   
    ...
        //检查LayoutParams是否合法,若不合法则使用默认值
        if (!checkLayoutParams(params)) { 
   
            params = generateLayoutParams(params);
        }
    ...
        //子View添加到mChildren数组
        addInArray(child, index);
    ...
}

//检查LayoutParams是否合法
@Override
protected boolean checkLayoutParams(ViewGroup.LayoutParams p) { 
   
    return p instanceof LinearLayout.LayoutParams;
}

@Override
public LayoutParams generateLayoutParams(AttributeSet attrs) { 
   
    return new LinearLayout.LayoutParams(getContext(), attrs);
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/191001.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • map遍历方式及效率_java遍历map集合

    map遍历方式及效率_java遍历map集合一、map遍历的效率先创建一个map,添加好数据:Map<String,String>map=newHashMap<>();for(inti=0;i<1000000;i++){map.put(i+"",i+"AA");}1、keySet的for循环方式://只获取keypublicstaticv…

    2022年9月21日
    5
  • Java学习书籍整理

    Java学习书籍整理不知不觉中,做Java开发已经四年了,趁着现在换了工作,还算比较闲,就整理一下自己学习的一些知识。在学习过程中,我喜欢找的一些电子书来系统化的学习,多数时候我都只是做一个大概的了解,没有深入学习,基本就是看下目录,看下前几章的内容,深入的只会在实际用的时候才去详细看了。我从以下几个方面整理了一下这些学习的电子书,分享给大家,免得到处去找这些资源,百度脑图里面的分类也…

    2022年6月15日
    24
  • 模型剪枝学习笔记 — EagleEye: Fast Sub-net Evaluation for Efficient Neural Network Pruning

    模型剪枝学习笔记 — EagleEye: Fast Sub-net Evaluation for Efficient Neural Network Pruning论文:https://arxiv.org/abs/2007.02491代码:https://github.com/anonymous47823493/EagleEye这篇论文一定要好好研究下,提出该剪枝方法的是暗物智能科技&中山大学,当初去面试过该公司,聊了将近一小时,大部分是关于剪枝的内容。。。。。。。可惜自己真实菜如狗。。。。。…

    2022年8月16日
    10
  • 软件测试常见面试题_软件测试面试题汇总

    软件测试常见面试题_软件测试面试题汇总Ⅰ.一个Bug1. 一条软件缺陷(或者叫Bug)记录都包含了哪些内容? 如何提交高质量的软件缺陷(Bug)记录?一条Bug记录最基本应包含:编号、Bug所属模块、Bug描述、Bug级别、发现日期、发现人、修改日期、修改人、修改方法、回归结果等等;要有效的发现Bug需参考需求以及详细设计等前期文档设计出高效的测试用例,然后严格执行测试用例,对发现的问题要充分确认肯定,然后再向外发布如此才能提高提交B…

    2022年8月27日
    5
  • vue-axios使用_vue接口请求放在哪里写

    vue-axios使用_vue接口请求放在哪里写什么是axiosAxios是一个基于promise的HTTP库,可以用在浏览器和node.js中。主要的作用:axios主要是用于向后台发起请求的,还有在请求中做更多是可控功能。a

    2022年7月29日
    28
  • mysql字符串截取拼接_mybatis截取字符串

    mysql字符串截取拼接_mybatis截取字符串用法:1.substring(str,pos)例子:str是被操作的字符串,pos表示是从哪个位置开始。如果pos为正,则下标从左到右从1开始记。如果pos为负,则下标从右到左从-1开始记。2.substring(strfrompos)例子:与1的用法相同,但是逗号变为from。3.substring(str,pos,len)例子:str是被操作的字符串,pos表示是从哪个位置开始。如果pos为正,则下标从左到右从1开始记。如果pos为负,则下

    2022年10月2日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号