Android之LayoutInflater三种方式分析

来源:互联网 发布:java抓取网页源码 编辑:程序博客网 时间:2024/05/16 01:40

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

未完待续

1 0
原创粉丝点击