View默认的LayoutParams是何时生成的,默认值是什么。layout_width和layout_height属性在哪里生效

简介: View默认的LayoutParams是何时生成的,默认值是什么。layout_width和layout_height属性在哪里生效

View默认的LayoutParams是何时生成的,默认值是什么

View#mLayoutParams属性:

/**
 * The layout parameters associated with this view and used by the parent
 * {@link android.view.ViewGroup} to determine how this view should be
 * laid out.
 * {@hide}
 */
protected ViewGroup.LayoutParams mLayoutParams;

它唯一的可以修改的地方是View#setLayoutParams(ViewGroup.LayoutParams params)方法.

如果我们不手动给View设置ViewGroup.LayoutParams属性,那它会有默认的值么?答案是有的。

添加View的两种方式

添加View一般有两种方式,一种是xml中添加,我们再通过View#findViewById()获取View;另一种是通过ViewGroup#addView()的一系列重载方法来添加。

xml添加

xml添加代码,一种是直接写到activity的xml布局文件中,通过Activity#setContentView()方法设置布局文件;另一种是将某个xml文件通过LayoutInflater#inflate方法解析成View,我们给Fragment设置布局文件或者自定义View时用的就是这种方式。

需说明的是,我们常用的View.inflate(Context context, int resource, ViewGroup root)方法,内部也是调用的LayoutInflater#inflate(int resource, ViewGroup root, boolean attachToRoot)方法。

LayoutInflater#inflate方法

我们先看下LayoutInflater#inflate方法:

public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot) {
    final Resources res = getContext().getResources();
    if (DEBUG) {
        Log.d(TAG, "INFLATING from resource: \"" + res.getResourceName(resource) + "\" ("
                + Integer.toHexString(resource) + ")");
    }

    final XmlResourceParser parser = res.getLayout(resource);
    try {
        return inflate(parser, root, attachToRoot);
    } finally {
        parser.close();
    }
}

这里主要分两步走,第一步根据布局文件生成XmlResourceParser对象,第二步调用inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot)方法把parser对象转换成View对象。

接着看inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot)方法,简单起见,删除了不必要的代码:

public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) {
        
    View result = root;

    // Look for the root node.
    int type;
    // 寻找根节点
    while ((type = parser.next()) != XmlPullParser.START_TAG &&
            type != XmlPullParser.END_DOCUMENT) {
        // Empty
    }

    final String name = parser.getName();
    if (TAG_MERGE.equals(name)) {
        rInflate(parser, root, inflaterContext, attrs, false);
    } else {
        // 1
        final View temp = createViewFromTag(root, name, inflaterContext, attrs);
        ViewGroup.LayoutParams params = null;
        // 2、3
        if (root != null) {
            // Create layout params that match root, if supplied
            params = root.generateLayoutParams(attrs);
            if (!attachToRoot) {
                temp.setLayoutParams(params);
            }
        }

        // Inflate all children under temp against its context.
        // 4
        rInflateChildren(parser, temp, attrs, true);

        if (root != null && attachToRoot) {
            root.addView(temp, params);
        }
        if (root == null || !attachToRoot) {
            result = temp;
        }
    }
    // 5
    return result;
}

这个方法很明确,穿入参数XmlPullParser和ViewGroup对象root(可为空),然后返回一个创建好的View。我们的任务是找到给新创建的View设置LayoutParams的地方。

我们只看我们关心的逻辑:

1、先通过createViewFromTag方法创建一个根View对象temp出来
2、如果root不为空,就通过root.generateLayoutParams(attrs)方法将temp的width和height属性转化成LayoutParams设置给temp。
3、如果root为空,表示temp的父布局不确定,这里也没有必要给设置LayoutParams了,等到它添加进别的布局时,就会设置LayoutParams参数了。
4、通过rInflateChildren方法,将temp的子View都添加进来
5、返回根view(temp是必定包含在根view中的)

接下来我们看下添加子View的rInflateChildren方法,它最终会调用到rInflate方法,老规矩,删除无关代码,只看关心的:

void rInflate(XmlPullParser parser, View parent, Context context,
        AttributeSet attrs, boolean finishInflate) throws XmlPullParserException, IOException {

    final int depth = parser.getDepth();
    int type;
    boolean pendingRequestFocus = false;

    while (((type = parser.next()) != XmlPullParser.END_TAG ||
            parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {

        final String name = parser.getName();

        if (TAG_REQUEST_FOCUS.equals(name)) {
            ...
        } else {
            final View view = createViewFromTag(parent, name, context, attrs);
            final ViewGroup viewGroup = (ViewGroup) parent;
            final ViewGroup.LayoutParams params = viewGroup.generateLayoutParams(attrs);
            rInflateChildren(parser, view, attrs, true);
            viewGroup.addView(view, params);
        }
    }

    if (finishInflate) {
        parent.onFinishInflate();
    }
}

1、开启while循环,根据获取到的属性,调用createViewFromTag方法生成View。createViewFromTag方法里面会通过反射,调用包含两个参数的构造器(形如View(Context context, @Nullable AttributeSet attrs))生成View对象。

2、通过ViewGroup#generateLayoutParams方法获取子View对应的attrs里面的宽高,也就是我们在布局中给View设置的android:layout_width和android:layout_height属性。根据这个宽高生成对应的LayoutParams参数,接着将view添加给对应的parent,添加过程中会将这个LayoutParams参数设置给生成的View对象(后面会讲解)。

3、在添加View之前,会递归调用rInflateChildren方法,完成当前View的子View的添加。

需要说明的是,这里的采用的是深度优先遍历的方式进行的创建。

我们再重点看下ViewGroup#generateLayoutParams方法是如何将子View的宽高生成LayoutParams参数的。

public LayoutParams generateLayoutParams(AttributeSet attrs) {
    return new LayoutParams(getContext(), attrs);
}

它调用了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();
}

这里通过Context和attrs获取R.styleable.ViewGroup_Layout属性集合,接着通过setBaseAttributes方法读取资源文件中的layout_width和layout_height属性,接着设置给LayoutParams的width和height属性。具体如下:

/**
 * Extracts the layout parameters from the supplied attributes.
 *
 * @param a the style attributes to extract the parameters from
 * @param widthAttr the identifier of the width attribute
 * @param heightAttr the identifier of the height attribute
 */
protected void setBaseAttributes(TypedArray a, int widthAttr, int heightAttr) {
    width = a.getLayoutDimension(widthAttr, "layout_width");
    height = a.getLayoutDimension(heightAttr, "layout_height");
}

setBaseAttributes方法将布局文件中的layout_width和layout_height属性值分别赋值给了LayoutParams的width和height属性,这样就完成了子View对应的LayoutParams的构建。

好了,通过LayoutInflater#innflate将xml转换成View的流程我们分析完了,每个子View在创建时都会设置LayoutParams属性,并且该属性都来源与子View的width和height属性。

Activity#setContentView方法

接下来我们研究下Activity#setContentView方法设置的xml,是如何转化成View对象的?转化过程中是如何添加LayoutParams属性的。

Activity#setContentView源码如下:

public void setContentView(@LayoutRes int layoutResID) {
    getWindow().setContentView(layoutResID);
    initWindowDecorActionBar();
}

这里的getWindow()的具体实现是PhoneWindow,我们看下PhoneWindow#setContentView(int layoutResID)的实现:

public void setContentView(int layoutResID) {
    ...
    if (hasFeature(FEATURE_CONTENT_TRANSITIONS)) {
        final Scene newScene = Scene.getSceneForLayout(mContentParent, layoutResID, getContext());
        transitionTo(newScene);
    } else {
        mLayoutInflater.inflate(layoutResID, mContentParent);
    }
    ...
}

可以看到最终还是调用了LayoutInflater#inflate方法将xml解析成View,并添加进到mContentParent中。LayoutInflater#inflate的具体实现可以参照上面的分析。

ViewGroup#addView()

我们看下ViewGroup#addView的几个重载方法:

addView(View child)
addView(View child, int index)
addView(View child, int width, int height)
addView(View child, LayoutParams params)
addView(View child, int index, LayoutParams params)

具体可以两类,一类是入参里面包含LayoutParams参数的,一类是不包含的。

入参包含LayoutParams的方法直接将LayoutParams设置给view即可;入参不包含LayoutParams需要生成一个默认的LayoutParams,这里以addView(View child, int index)方法为例,我们看下它的实现:

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();
    if (params == null) {
        params = generateDefaultLayoutParams();
        if (params == null) {
            throw new IllegalArgumentException("generateDefaultLayoutParams() cannot return null");
        }
    }
    addView(child, index, params);
}

可以看出,如果view没有设置过LayoutParams,就通过generateDefaultLayoutParams()方法生成一个,我们看下默认生成的LayoutParams是什么样的:

protected LayoutParams generateDefaultLayoutParams() {
    return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
}

可以看出,默认的LayoutParams中宽高给的都是wrap_content。

总结

通过上面的分析,可以得出结论:
1、通过xml布局文件生成的View对象,会默认添加LayoutParams属性,它的属性值主要来源于子布局的width和height属性。
2、通过ViewGroup#addView()方法添加的View,如果View没有LayoutParams属性,默认会给添加LayoutParams属性,它的属性值默认都是wrap_content。

相关文章
|
2月前
|
弹性计算 负载均衡 安全
阿里云国际站服务代理商:SLB批量采购怎么做预算?按地域还是业务系统拆分
阿里云国际站SLB批量采购预算需兼顾地域(定价/部署)与业务系统(成本归属)双维度,避免重复计费;ALB/NLB/CLB计费模型不同,不可混用单价;应按实际资源清单(产品类型、网络类型、环境、周期)精细化报价与对账。
|
3月前
|
缓存 Linux iOS开发
DeepSeek Harness 自动更新怎么开?dsh 本体自动更新与插件升级提醒的做法
DeepSeek Harness 本体没有自动更新开关:最省事是用 npx 启动(每次按最新解析),全局安装则用系统计划任务定时跑 npm install -g @latest;插件侧另有启动时检查更新与可更新徽标。
701 1
DeepSeek Harness 自动更新怎么开?dsh 本体自动更新与插件升级提醒的做法
|
4月前
|
数据采集 人工智能 自然语言处理
# 企业Agent为什么不能只靠RAG?RAG、规则引擎和Workflow的职责边界与架构设计
企业Agent不能仅靠RAG,因其仅解决“知道什么”;真实业务还需Rule Engine(确定性判断)、Workflow(任务编排)、LLM(理解表达)、业务系统(执行落地)与治理机制(权限风控),共同构成可信赖的企业智能能力底座。(239字)
|
2月前
|
人工智能 自然语言处理 监控
从问数工具到运营搭档:慧博科技基于Qoder Cloud Agents打造“小慧Claw”
慧博科技推出AI运营助手“小慧Claw”,基于Qoder Cloud Agents(QCA)构建智能体操作系统,实现从Prompt到Harness的范式升级。它支持自然语言问数、智能报表、经营预警等核心场景,将重复性运营工作自动化,使运营人员聚焦决策。接入QCA后,任务响应从小时级降至分钟级,报表口径准确率提升至95%,人效提升5倍。
339 0
|
6月前
|
人工智能 缓存 监控
构建企业级 AI Agent 工程化实践:从原型到生产环境的跨越
本文深入探讨企业级AI Agent从原型到生产的工程化实践,直面LLM概率性与业务确定性的根本矛盾,提出“LLM负责感知推理、代码保障逻辑执行”的混合架构。系统阐述可观测性、安全护栏、性能优化、数据管理四大工程支柱,并结合IT运维、金融合规等实战场景,提供可落地的LLMOps方法论。
|
8月前
|
JSON 安全 Java
【SpringSecurity新手村系列】(6)基于角色的权限控制、权限拦截注解与自定义无权限页面
本章在 RBAC 角色体系上,开启 @EnableMethodSecurity,使用 @PreAuthorize 配合 hasRole / hasAnyRole 实现 Controller 方法级拦截,并配置自定义 403 无权限页面,重点拆解 ROLE_ 前缀重复拼接的常见坑位。
567 1
|
6月前
|
人工智能 自然语言处理 API
AI时代,大学生如何从就业走向创业?
AI时代,大学生可走“先就业、再创业”新路径:以AI智能体为杠杆,在职场成长为“一人部门型员工”,积累案例、客户与商业认知,再升级为面向市场的“一人公司”。本文详解三阶段跃迁、三大核心能力及开源社区支持资源。
|
11月前
|
人工智能 自然语言处理 运维
企业数据分析系统建设费用,2026年降本增效的三大趋势
2026年,数据分析系统成企业降本增效战略刚需。全球投资达3850亿美元,中国占比超620亿美元(+18.7%)。瓴羊Quick BI以AI原生架构、“智能小Q”自然语言分析、多源实时接入及SaaS/私有化/混合云灵活部署,助力企业实现低门槛、高ROI的数据智能转型。(239字)
企业数据分析系统建设费用,2026年降本增效的三大趋势
|
9月前
|
人工智能 Linux API
OpenClaw刷屏全网:AI智能体落地,易用性才是开发者与企业的核心诉求
本文剖析火爆社区的AI智能体框架OpenClaw(“龙虾”):肯定其开源灵活、支持多工具联动等创新,更直指其云上部署门槛高、插件生态弱、场景适配窄三大短板。对比提出阿里云深度适配的玄晶引擎——一键部署、视窗操作、全场景覆盖、免插件开发,真正实现低门槛、高可用的AI智能体云上落地。
903 5
|
11月前
|
存储 弹性计算 关系型数据库
阿里云服务器ECS通用算力型u1实例ecs.u1-c1m2.large规格族收费199元一年(2核4G配置)
阿里云企业专享199元/年,2核4G、5M固定带宽、80G ESSD Entry云服务器,性能稳定,限企业用户限购1台,续费同价,适合轻量级企业应用与对外服务。