首页 > 移动平台 > 详细

Android之LayoutInflater三种方式分析

时间:2015-04-10 18:05:10      阅读:113      评论:0      收藏:0      [点我收藏+]

获取LayoutInflater有三种不同的方式,那么这三种方式有什么区别呢?

源码:

LayoutInflater inflater = LayoutInflater.from(context);  (LayoutInflater类)

<span style="font-size:14px;">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;
  }</span>

 LayoutInflater inflater = getLayoutInflater();(Activity类) 

<span style="font-size:14px;color:#330033;">public LayoutInflater getLayoutInflater() {
       return getWindow().getLayoutInflater();
}</span>

public PhoneWindow(Context context) {
         super(context);
         mLayoutInflater = LayoutInflater.from(context);
     }


③ LayoutInflater inflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);(Activity类) 

<span style="font-size:14px;color:#330033;"> public static final String LAYOUT_INFLATER_SERVICE = "layout_inflater";</span>
ps: Use with getSystemService(java.lang.String) to retrieve a android.view.LayoutInflater for inflating layout resources in this context.

从源码上,三种方式在本质都是一样的,②是调用①的方法,而①则是在调用③。java很有意思就是这样子调来调去,给人一种似是而非的感觉。

对于getSystemService类,我们在源码中可以看到提供了很多的Service:

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位置的服务
SEARCH_SERVICE SearchManager搜索的服务
VEBRATOR_SERVICE Vebrator手机震动的服务
CONNECTIVITY_SERVICE Connectivity网络连接的服务
WIFI_SERVICE WifiManagerWiFi服务
TELEPHONY_SERVICE TeleponyManager电话服务

未完待续

Android之LayoutInflater三种方式分析

原文:http://blog.csdn.net/violetic/article/details/44980757

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!