LayoutInflater和LayoutParams

本文深入解析LayoutInflater的工作原理,包括其在Android系统中的注册与实现过程,以及PhoneLayoutInflater类的onCreateView方法如何创建View。同时,阐述了LayoutParams类的功能,它是如何在ViewGroup中为子视图设置布局参数,包括高度、宽度及margin属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一.LayoutInflater

开发时,很多时候会用到LayoutInflater来加载指定的布局

LayoutInflater inflater = (LayoutInflater)context.getSystemService
       (Context.LAYOUT_INFLATER_SERVICE);
// 或者通过from(Context context)获取实例
LayoutInflater inflater = LayoutInflater.from(context);

inflater.inflate(int resource, ViewGroup root);

在frameworks的SystemServiceRegistry.java中注册了此服务

        registerService(Context.LAYOUT_INFLATER_SERVICE, LayoutInflater.class,
                new CachedServiceFetcher<LayoutInflater>() {
            @Override
            public LayoutInflater createService(ContextImpl ctx) {
                return new PhoneLayoutInflater(ctx.getOuterContext());
            }});

new PhoneLayoutInflater,PhoneLayoutInflater继承了LayoutInflater

LayoutInflater是一个抽象类:

public abstract class LayoutInflater {
    ....
}

PhoneLayoutInflater:

public class PhoneLayoutInflater extends LayoutInflater {
    private static final String[] sClassPrefixList = {
        "android.widget.",
        "android.webkit.",
        "android.app."
    };

    /**
     * Instead of instantiating directly, you should retrieve an instance
     * through {@link Context#getSystemService}
     *
     * @param context The Context in which in which to find resources and other
     *                application-specific things.
     *
     * @see Context#getSystemService
     */
    public PhoneLayoutInflater(Context context) {
        super(context);
    }

    protected PhoneLayoutInflater(LayoutInflater original, Context newContext) {
        super(original, newContext);
    }

    /** Override onCreateView to instantiate names that correspond to the
        widgets known to the Widget factory. If we don't find a match,
        call through to our super class.
    */
    @Override protected View onCreateView(String name, AttributeSet attrs) throws ClassNotFoundException {
        for (String prefix : sClassPrefixList) {
            try {
                View view = createView(name, prefix, attrs);
                if (view != null) {
                    return view;
                }
            } catch (ClassNotFoundException e) {
                // In this case we want to let the base class take a crack
                // at it.
            }
        }

        return super.onCreateView(name, attrs);
    }

    public LayoutInflater cloneInContext(Context newContext) {
        return new PhoneLayoutInflater(this, newContext);
    }
}

可以看到PhoneLayoutInflater实现了onCreateView()方法,当调用LayoutInflater的inflate()时,最终就会调用onCreateView(),从而创建出view

public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) {
    ......
    final View temp = createViewFromTag(root, name, inflaterContext, attrs);
    ......
}


View createViewFromTag(View parent, String name, Context context, AttributeSet attrs,
            boolean ignoreThemeAttr) {
    ......
    try {
        if (-1 == name.indexOf('.')) {
            view = onCreateView(parent, name, attrs);
        } else {
            view = createView(name, null, attrs);
        }
    } finally {
        mConstructorArgs[0] = lastContext;
    }
    ......
}

protected View onCreateView(String name, AttributeSet attrs)
         throws ClassNotFoundException {
     return createView(name, "android.view.", attrs);
} 

二.LayoutParams

LayoutParams继承于Android.View.ViewGroup.LayoutParams,是ViewGroup的一个内部抽象类,封装了Layout的位置、高、宽等信息

LayoutParams类是用于child view(子视图)向parent view(父视图)传达自己的意愿的一个东西

ViewGroup.LayoutParams类只能简单的设置高height以及宽width两个基本的属性,宽和高都可以设置成三种值:

       1,FILL_PARENT,一个确定的值;

       2,MATCH_PARENT;

       3,WRAP_CONTENT。

ViewGroup.MarginLayoutParams类是ViewGroup.LayoutParams的子类,顾名思义,它只能设置child View的margin属性信息。

     1、利用setMargins(left,top,right,bottom);

     2、利用MarginLayoutParams对象params方法单独设置.topMargin

基本使用:

    常用子类的简单使用:LinearLayout.LayoutParams、RelativeLayout.Params以及WindowManager.Params。

例子:

            LayoutParams param = new LayoutParams(
                    0, LayoutParams.MATCH_PARENT);
            //param.gravity = Gravity.CENTER_VERTICAL;
            param.weight = 1;
            view.setLayoutParams(param);
            addView(view,i);

 

 

<think>我们正在讨论LayoutInflater.from(context)方法的作用使用示例。 根据引用[2]引用[3],LayoutInflater.from(context)是获取LayoutInflater实例的三种方式之一。 引用[4]展示了在自定义View中使用LayoutInflater.from(context).inflate()的示例。 用户的问题:我想理解或获取关于 `LayoutInflater.from(mContext)` 的使用方法或作用 回答步骤: 1. 解释LayoutInflater.from(context)的作用:它是一个静态方法,用于获取LayoutInflater的实例。 2. 说明参数:需要传入一个Context对象(如Activity、Application等)。 3. 与其他获取方式对比:引用[2]提到三种方式,其中LayoutInflater.from(context)是最简洁的方式。 4. 使用示例:展示如何使用获取到的LayoutInflater实例来加载布局。 注意:引用[4]中详细展示了inflate方法的使用,特别是第二个参数(root)第三个参数(attachToRoot)的不同效果。 我们还可以结合用户之前的问题(关于convertView的inflate)来进一步说明在适配器中的使用。 因此,我们可以这样组织回答: - 作用 - 获取方式对比 - 使用示例(包括在自定义View适配器中的使用) - 注意事项(如attachToRoot参数) 同时,根据用户之前的对话,用户可能是在适配器的getView方法中使用,所以可以结合适配器的场景说明。 最后,生成相关问题。</think>### `LayoutInflater.from(mContext)` 详解 `LayoutInflater.from(mContext)` 是 Android 中获取 `LayoutInflater` 实例的标准方式,用于将 XML 布局文件转换为实际的 `View` 对象。以下是核心要点: #### 一、作用与原理 1. **作用** 通过 `Context` 创建 `LayoutInflater` 实例,用于动态加载 XML 布局资源。 2. **内部实现** 等价于: ```java LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); ``` 本质是通过系统服务获取布局解析器实例[^2][^3]。 3. **优势** - **简洁性**:相比 `getSystemService()` 更简洁 - **通用性**:可在任何拥有 `Context` 的地方使用(Activity/Fragment/Adapter等) - **安全性**:避免直接操作系统服务 #### 二、使用示例 ##### 1. 基础用法(加载单个视图) ```java // 创建布局解析器 LayoutInflater inflater = LayoutInflater.from(mContext); // 加载布局(不立即附加到父视图) View customView = inflater.inflate( R.layout.custom_layout, // 布局资源ID parentView, // 父容器(用于继承布局参数) false // 不立即附加 ); // 手动添加到父容器 parentView.addView(customView); ``` ##### 2. 在适配器中的用法(复用视图) ```java @Override public View getView(int position, View convertView, ViewGroup parent) { // 获取 LayoutInflater LayoutInflater inflater = LayoutInflater.from(parent.getContext()); if (convertView == null) { // 首次创建视图 convertView = inflater.inflate( R.layout.list_item, parent, false // 必须为false,由适配器管理附加 ); // 初始化ViewHolder... } // 更新视图数据 return convertView; } ``` #### 三、关键参数解析 | 参数 | 说明 | 推荐场景 | |------|------|----------| | `resource` | XML 布局资源 ID | 必填 | | `root` | 父容器 (`ViewGroup`) | **必填** (确保布局参数生效) | | `attachToRoot` | 是否立即附加 | `false` (适配器/动态添加)<br/>`true` (自定义View组件)[^4] | #### 四、注意事项 1. **必须传递 `root` 参数** 未传递时布局的 `layout_width/layout_height` 等参数会失效: ```java // 错误!布局参数可能异常 inflater.inflate(R.layout.item, null); // 正确 inflater.inflate(R.layout.item, parent, false); ``` 2. **上下文选择** 优先使用 `parent.getContext()` 而非 `Activity` 上下文,避免内存泄漏: ```java // 在适配器中推荐 LayoutInflater.from(parent.getContext()); ``` 3. **性能优化** 在 `RecyclerView/ListView` 中务必复用 `convertView` + `ViewHolder` 模式,避免频繁调用 `inflate()`。 > 总结:`LayoutInflater.from(context)` 是获取布局解析器的标准方式,核心价值在于将静态 XML 转换为可操作的 `View` 对象,同时确保布局参数正确继承[^1][^4]。 --- ### 相关问题 1. **为什么 `inflate()` 的 `attachToRoot` 参数在适配器中必须设为 `false`?** [答案:适配器需自主管理视图生命周期,设为 `true` 会导致重复添加视图引发异常[^4]。] 2. **`LayoutInflater.from()` `Activity.getLayoutInflater()` 有何区别?** [答案:前者基于 `Context` 更通用,后者是 `Activity` 的快捷方法,本质相同[^2]。] 3. **如何在自定义 View 组件中正确使用 `LayoutInflater`?** [答案:构造函数中调用 `inflate(R.layout.xxx, this, true)` 实现自动附加[^4]。] 4. **为什么通过 `LayoutInflater` 加载的视图有时布局参数失效?** [答案:未传递有效的 `root` 参数导致无法解析 `LayoutParams`[^4]。]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值