LayoutInflater使用

本文详细解释了Android开发中LayoutInflater的作用与用法,包括如何动态加载界面、获取布局文件中的视图元素,以及inflate方法的具体实现与参数应用。通过简单的例子,展示了不同参数设置对视图加载与布局的影响。

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

一. LayoutInflater
在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById()。不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例化;而findViewById()是找xml布局文件下的具体widget控件(如Button、TextView等)。
具体作用:
1、对于一个没有被载入或者想要动态载入的界面,都需要使用LayoutInflater.inflate()来载入;
2、对于一个已经载入的界面,就可以使用Activiyt.findViewById()方法来获得其中的界面元素。
LayoutInflater 是一个抽象类,在文档中如下声明:
public abstract class LayoutInflater extends Object
获得 LayoutInflater 实例的三种方式
1. LayoutInflater inflater = getLayoutInflater();//调用Activity的getLayoutInflater() 
2. LayoutInflater inflater = LayoutInflater.from(context);  
3. LayoutInflater inflater =  (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
其实,这三种方式本质是相同的,从源码中可以看出:
getLayoutInflater():
Activity 的 getLayoutInflater() 方法是调用 PhoneWindow 的getLayoutInflater()方法,看一下该源代码:
public PhoneWindow(Context context)
{   
 super(context);   
    mLayoutInflater = LayoutInflater.from(context);
}
可以看出它其实是调用 LayoutInflater.from(context)。
LayoutInflater.from(context):
public static LayoutInflater from(Context context)
{   
 LayoutInflater LayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (LayoutInflater == null)
    {       
     throw new AssertionError("LayoutInflater not found.");   
    }   
    return LayoutInflater;
}
可以看出它其实调用 context.getSystemService()。
结论:所以这三种方式最终本质是都是调用的Context.getSystemService()。
另外getSystemService()是Android很重要的一个API,它是Activity的一个方法,根据传入的NAME来取得对应的Object,然后转换成相应的服务对象。以下介绍系统相应的服务。
 
传入的Name 返回的对象 说明
WINDOW_SERVICE WindowManager 管理打开的窗口程序
LAYOUT_INFLATER_SERVICE LayoutInflater 取得xml里定义的view
ACTIVITY_SERVICE ActivityManager 管理应用程序的系统状态
POWER_SERVICE PowerManger 电源的服务
ALARM_SERVICE AlarmManager 闹钟的服务
NOTIFICATION_SERVICE NotificationManager 状态栏的服务
KEYGUARD_SERVICE KeyguardManager 键盘锁的服务
LOCATION_SERVICE LocationManager 位置的服务,如GPS
SEARCH_SERVICE SearchManager 搜索的服务
VEBRATOR_SERVICE Vebrator 手机震动的服务
CONNECTIVITY_SERVICE Connectivity 网络连接的服务
WIFI_SERVICE WifiManager Wi-Fi服务
TELEPHONY_SERVICE TeleponyManager 电话服务

二. inflate 方法
通过 sdk 的 api 文档,可以知道该方法有以下几种过载形式,返回值均是 View 对象,如下:
public View inflate (int resource, ViewGroup root) 
public View inflate (XmlPullParser parser, ViewGroup root)
public View inflate (XmlPullParser parser, ViewGroup root, boolean attachToRoot)  
public View inflate (int resource, ViewGroup root, boolean attachToRoot)

LayoutInflater的inflate方法一共有四种,但我们日常用经常用到的就只有这两种:
    public View inflate(int resource, ViewGroup root) {
        return inflate(resource, root, root != null);
    }
    public View inflate(int resource, ViewGroup root, boolean attachToRoot) {
        if (DEBUG) System.out.println("INFLATING from resource: " + resource);
        XmlResourceParser parser = getContext().getResources().getLayout(resource);
        try {
            return inflate(parser, root, attachToRoot);
        } finally {
            parser.close();
        }
    }
这两个方法都最终调用到: public View inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot)
所以,这里直接介绍里面调用这个方法即可:
在这个方法里面,上半部分为xml解析的代码,这里就不贴出来,确实没什么东西可看。直接看中间部分的代码:
                    ViewGroup.LayoutParams params = null;

                    if (root != null) {
                        if (DEBUG) {
                            System.out.println("Creating params from root: " +
                                    root);
                        }
                        // Create layout params that match root, if supplied
                        params = root.generateLayoutParams(attrs);
                        if (!attachToRoot) {
                            // Set the layout params for temp if we are not
                            // attaching. (If we are, we use addView, below)
                            temp.setLayoutParams(params);
                        }
                    }

params = root.generateLayoutParams(attrs);

这段的意思是:如果调用inflate方法,传入了ViewGroup root参数,则会从root中得到由layout_width和layout_height组成的LayoutParams,在attachToRoot设置为false的话,就会对我们加载的视图View设置该LayoutParams。

接着往下看:
                    // We are supposed to attach all the views we found (int temp)
                    // to root. Do that now.
                    if (root != null && attachToRoot) {
                        root.addView(temp, params);
                    }

                    // Decide whether to return the root that was passed in or the
                    // top view found in xml.
                    if (root == null || !attachToRoot) {
                        result = temp;
                    }

root.addView(temp, params);

如果设置了ViewGroup root参数,且attachToRoot设置为true的话,则将我们加载的视图做为子视图添加到root视图中。

接下来,就用最最简单的例子来说明一下:

用两个布局文件main 和 test:

其中,main.xml文件为:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:gravity="center"
        android:text="hello world" />

</LinearLayout>

test.xml文件为:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:background="#ffffff00"
    android:orientation="vertical" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:gravity="center"
        android:text="test" />

</LinearLayout>
在test中设置了其高度为200dp,并且设置了背景颜色。

接下来看一下LayoutInflater().inflate方法实现:

第一种方式:inflate(view, null)

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View view = (LinearLayout) getLayoutInflater().inflate(R.layout.main,
                null);

        view = getLayoutInflater().inflate(R.layout.test, null);

        setContentView(view);
    }

运行的效果如下:


这个就很容易理解了,因为我没有指定ViewGroup root参数,所以,相当于直接加载了test视图文件,并返回。

而它的高度充满了全屏而不是200dp,因为执行inflate的时候,没有root参数,则无法为test视图设定layoutparam参数。那么为什么会充满屏幕而不是没有显示呢?是因为我们将其设置视图到activity时,会取得当前window的layoutparam赋值给它,也就是充满全屏。有兴趣的话,你可以改一下test的layout_width设定一个数值,最后运行效果是一样的。


第二种方式:inflate(view, root, false)

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View view = (LinearLayout) getLayoutInflater().inflate(R.layout.main,
                null);

        view = getLayoutInflater().inflate(R.layout.test, (ViewGroup) view, false);

        setContentView(view);
    }

这里调用inflate的时候,强转了view为viewgroup,因为其本身就是linearlayout,所以这里可以强转。

运行的效果如下:

单看效果而言,跟上面的一样。但从代码本身而言,实现的内容就不一样了。由于有了viewgroup,这里得到的视图其实已经有了layoutparam,你可以自行打印Log看看。

但为什么最后的结果却是和上面的一样呢。原因还是由于设置视图到activity时,会取得当前window的layoutparam赋值给它,也就是充满全屏。


第三种方式:inflate(view, root, true)   

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View view = (LinearLayout) getLayoutInflater().inflate(R.layout.main,
                null);

        view = getLayoutInflater().inflate(R.layout.test, (ViewGroup) view,
                true);

        setContentView(view);
    }
运行的效果如下:

这个效果就很明显了,由于main是线性布局,所以,test视图被添加到了textview(hello world)下面,并且保留了其自己的layoutparam参数。

另外其布局上的控件可以用下面示意代码获取:
LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);       
View view = inflater.inflate(R.layout.custom, (ViewGroup)findViewById(R.id.test));       
<span style="color:#FF0000;">//EditText editText = (EditText)findViewById(R.id.content);// error </span>
EditText editText = (EditText)view.findViewById(R.id.content);

对于上面代码,指定了第二个参数 ViewGroup root,当然你也可以设置为 null 值。
注意:
·inflate 方法与 findViewById 方法不同;
·inflater 是用来找 res/layout 下的 xml 布局文件,并且实例化;
·findViewById() 是找具体 xml 布局文件中的具体 widget 控件(如:Button、TextView 等)。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值