Android LayoutParams源码分析

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

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/220295.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月17日 下午8:48
下一篇 2026年3月17日 下午8:48


相关推荐

  • Yeah,我的Pygame安装成功了!(Windows Pygame 安装教程)

    Yeah,我的Pygame安装成功了!(Windows Pygame 安装教程)最近在自学python,在看完了些基础知识之后,准备写个小项目,我对照的教程是《python编程-从入门到实践》。在这本书的第二部分项目篇的第一个小项目是【外星人入侵】,这个项目需要安装pygame,书中有给出了两个下载链接:https://bitbucket.org/pygame/pygame/downloads/和http://www.lfd.uci.edu/~gohlke/pythonl…

    2022年5月10日
    52
  • CreateEvent方法详解

    CreateEvent方法详解HANDLECreateEvent(  LPSECURITY_ATTRIBUTESlpEventAttributes,//安全属性  BOOLbManualReset,//复位方式  BOOLbInitialState,//初始状态  LPCTSTRlpName//对象名称);调用示例:hEvent=CreateEvent(NULL,TRUE,…

    2022年7月12日
    26
  • 定了!“千问”成阿里旗下AI品牌统称

    定了!“千问”成阿里旗下AI品牌统称

    2026年3月13日
    2
  • 对称加密算法和非对称加密算法介绍

    对称加密算法和非对称加密算法介绍本文主要介绍对称加密算法 symmetricenc 和非对称加密算法 asymmetriccr 的相关知识 1 对称加密算法 1 1 概述对称加密算法是应用较早的加密算法 技术成熟 在对称加密算法中 数据发送方利用加密密钥 通过指定的加密算法将明文 原始数据 加密处理后 使明文加密为密文 然后发送出去 数据接收方在收到密文后 需要使用加密时使用的密钥 以及加密算法的逆算法对该密文进行解密 才能对应的明文 在对称

    2026年3月17日
    1
  • 轻松几步完成Openclaw云桌面部署并接入飞书

    轻松几步完成Openclaw云桌面部署并接入飞书

    2026年3月15日
    2
  • 递归下降算法_递归下降分析程序得到的经验

    递归下降算法_递归下降分析程序得到的经验递归下降算法算法模型:Term=Term+ExprExpr=Expr+FactorFactor=单个元素。最小单位。 实现原理:一个程式进入算法及被看作是一个项,分解成项加表达式的形式,表达式被分解成表达式加因子的形式,因子是这个算法中的最小单位。上一级调用比自己小一级的自己。这里三层分离,越下层模型中所形成的优先级就会越高。 我用递归下降算法写了个简单的计算器,递归算法为我的运算符号…

    2022年4月20日
    79

发表回复

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

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