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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 常用信息收集方法[通俗易懂]

    常用信息收集方法[通俗易懂]信息收集的种类信息收集分为被动收集和主动收集两种方式。被动信息收集:利用第三方的服务对目标进行访问:Google搜索、Shodan搜索、其他综合工具,被动信息收集是指京可能多低收集与目标相关的信息主动信息收集:通过直接扫描目标主机或者网站,主动方式能获取更多的信息,目标系统可能会记录操作信息。在信息收集中,需要收集的信息:目标主机的DNS信息、目标IP地址、子域名、旁站和C段、CMS类型、敏感目录、端口信息、操作系统版本、网站架构、漏洞信息、服务器与中间件信息、邮箱、人员、地址等。在信息收集中

    2022年6月17日
    70
  • leetcode-172. 阶乘后的零[通俗易懂]

    leetcode-172. 阶乘后的零[通俗易懂]给定一个整数 n,返回 n! 结果尾数中零的数量。示例 1:输入: 3输出: 0解释: 3! = 6, 尾数中没有零。示例 2:输入: 5输出: 1解释: 5! = 120, 尾数中有 1 个零.说明: 你算法的时间复杂度应为 O(log n) 。class Solution {public: int trailingZeroes(int n) { int ans = 0; for(int i = 5;i <= n;i += 5){

    2022年8月9日
    5
  • snmp trap服务_SNMP服务

    snmp trap服务_SNMP服务浅谈Linux中开发SNMPTrapSNMPTrap是一种标准的报告机制,广泛应用在各种网络管理软件中。本文详细介绍了SNMPTrap的基本概念,以及Linux下面net-snmp对SNMPTrap的开发与应用。1.SNMPTrap简介SNMP简单网络管理协议(SimpleNetworkManagementProtocol)是一种应用层协议,是TCP/IP协议族的一部分。它使网络设备之间能…

    2022年8月22日
    7
  • evolution configure

    evolution configure

    2022年3月11日
    45
  • 使用python+django+twistd 开发自己的操作和维护系统的一个

    使用python+django+twistd 开发自己的操作和维护系统的一个

    2022年1月2日
    43
  • Android之rild进程启动源码分析「建议收藏」

    Android之rild进程启动源码分析「建议收藏」Android电话系统框架介绍在android系统中rild运行在AP上,AP上的应用通过rild发送AT指令给BP,BP接收到信息后又通过rild传送给AP。AP与BP之间有两种通信方式:1.SolicitedResponse:Ap向Bp发送请求,Bp给Ap发送回复,该类型的AT指令及其回调函数以数组的形式存放在Ril_commands.h文件中:{数组中的索引号,请

    2025年9月20日
    5

发表回复

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

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