您的位置:首页 > 移动开发 > Android开发

总结:LayoutInflater和inflate()区别与联系 应用

2014-04-09 01:56 246 查看
首先说明的是LayoutInflater和inflate()这两个东东的区别,LayoutInflater是一个公共的抽象类,由object继承而来,而inflate()是LayoutInflater类的类方法,这一定要弄清楚概念,否则你会晕头转向。然后说这个东东的作用,我们先看看Google的综述:

Instantiates a layout XML file into its corresponding View objects. It is never used directly. Instead, use getLayoutInflater() or getSystemService(String) to retrieve a standard LayoutInflater instance that is already hooked up to the current context and correctly
configured for the device you are running on. For example:LayoutInflater inflater = (LayoutInflater)context.getSystemService (Context.LAYOUT_INFLATER_SERVICE);实例化一个Layout XML文件到它相应的视图对象。它不能直接使用。而是使用getLayoutInflater()或者getSystemService(String)去得到一个可以联系到当前的上下文和正确的配置到你正在运行的设备的标准的LayoutInflater实例,例如:LayoutInflater
inflater = (LayoutInflater)context.getSystemService (Context.LAYOUT_INFLATER_SERVICE);
以前的技术博客都在说:"实际开发种LayoutInflater这个类还是非常有用的,它的作用类似于 findViewById()",我觉得有点问题,因为LayoutInflater是一个类,而findViewById()是Activity的一个方法,所以不能这样的类比,不过到可以这样
说 “LayoutInflater类的类方法inflate()作用类似于findViewById()”。inflate()和findViewById()返回的均是一个View类,我认为这样或许更合适。

1.LayoutInflater类的类方法inflate()的作用是将Layout的XML布局文件实例化为View类对象。

我们首先获取LayoutInflater,有三种方式获取LayoutInflater:

(1).通过SystemService获得

    LayoutInflater inflater = (LayoutInflater)context.getSystemServices(Context.LAYOUT_INFLATER_SERVICES);

    View view = inflater.inflate(R.layout.main, null);

.LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 得到LayoutInflater类的对象inflater(这个对象名称是假定的,可以任意),android的后台运行在很多service,它们在系统启动时被SystemServer开启,支持系统的正常工作,比如MountService监听是否有SD卡安装及移除,ClipboardService提供剪切板功能,PackageManagerService提供软件包的安装移除及查看等等,应用程序可以通过系统提供的Manager接口来访问这些Service提供的数据。

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                  电话服务

(2).从给定的context中获得 LayoutInflater inflater = LayoutInflater.from(context); View view = inflater.inflate(R.layout.mian, null);

Google给的详细的方法介绍static LayoutInflater from(Context context);Obtains the LayoutInflater from the given context.从给定的上下文获得LayoutInflater,from()是LayoutInflater类的一个公共的方法,inflate()也是LayoutInflater类的一个公共的方法。所以 from()和inflate()都是为LayoutInflater服务的

(3). LayoutInflater inflater =getLayoutInflater();在Activity中可以使用,实际上是View子类下window的一个公共的抽象的方法,返回LayoutInflater类型的对象,而findViewById()也是View类的子类window的一个类方法,返回View类型的对象。    View view = inflater.inflate(R.layout.main, null);

三种方式中:第一种和第二种其实是一样的,只不过第二种稍有点比第一种繁琐一点,建议使用第一种,减少代码的冗余。其实第一种和第二种都是首先获得上下文,然后通过方法来获得LayoutInflater类的类对象。

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;

    }

2.View view = inflater.inflate(R.layout.custom_dialog, null);使用LayoutInflater类对象inflater的方法inflate,把动态载入的界面的XML文件载入到View类对象view中,此方法返回的是一个View类对象view(这个对象名称是假定的,可以任意)。关于这个类方法Google给了四种:

①View inflate(int resource, ViewGroup root)

Inflate a new view hierarchy from the specified xml resource.

②View inflate(XmlPullParser parser, ViewGroup root)

Inflate a new view hierarchy from the specified xml node.

③View inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot)

Inflate a new view hierarchy from the specified XML node.

④View inflate(int resource, ViewGroup root, boolean attachToRoot)

Inflate a new view hierarchy from the specified xml resource.

我们终于得到了View类对象view!!!

3.TextView text = (TextView) view.findViewById(R.id.text);

text.setText("Hello, this is my zone!");

ImageView image = (ImageView) view.findViewById(R.id.image);

image.setImageResource(R.drawable.icon);

这里你可以定义很多的控件,你可以展开你的想象力,但是提示一点的是:我们平常都是这样的ImageView image = (ImageView) findViewById(R.id.image1); 大家可能不理解为什么这里出现了view这个东东,之所以出现是因为在第二步中我们得到了View类对象view,我们的控件都是在view里面的所以我们需要加上view;必须用inflate()先将对话框上的layout找出来,然后再用这个layout对象去找到它上面的组件,如上面所示。

我们这步定义了view里面的控件们!!!

4.其实后面的就是上下文和构造器的神马东东了,该睡了。明天整理吧!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android LayoutInflater