`
阿七无可取代
  • 浏览: 1811 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

LayoutInflater类使用理解小结

阅读更多
前段时间学习,发现好多地方用到了 LayoutInflater这个类,我小结一下,希望给大家也给自己以后学习带来帮助。
LayoutInfalter 看api解释 This class is used to instantiate layout XML file into its corresponding View objects. 作用相当于 类.findViewById();不过前者是实例化xml文件(多用于局部实例化,另外的布局文件实例化,经常和适配器结合使用返回当前的view),后者实例化view控件, inflater 可以理解为扩展,放大。

实例化方法在api有两种,看源代码发现有三种。
第一种方法:
LayoutInflater inflater = LayoutInflater.from(this);
View layout = inflater.inflate(R.layout.main, null);
源码中:
    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;
    }

其实第一种方法,其实调用的就是第三种方法。

第二种方法:
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.main, null);
第三种方法:
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.main, null);

代码:
aa.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
 
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/icon"
>
</ImageView>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="aaaaaaaa"
>
</TextView>
</LinearLayout>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
   <Button
   android:id="@+id/button_1"
   android:layout_width="wrap_content"
    android:layout_height="wrap_content"
   />
  
</LinearLayout>
MainActivity.java
package com.aqi.app;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {
private Button button = null;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        button = (Button) findViewById(R.id.button_1);
        button.setText("局部对话框");
        button.setOnClickListener(new  View.OnClickListener() {
public void onClick(View v) {
Builder diaLog = new AlertDialog.Builder(MainActivity.this);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout =  inflater.inflate(R.layout.aa, null);
diaLog.setView(layout);
diaLog.create();
diaLog.show();
}
});
    }
}


网上找了点儿:
另外补充下,getSystemService是Activity中的方法,根据传入的name来取得对应的服务对象,这些服务名称参数都是Context类中的常量:

传入的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 电话服务
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics