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

Android学习之LayoutInflater类和inflate()方法的使用

2015-07-07 10:35 405 查看
1.首先来说明一下LayoutInflater这个类中inflate()这个方法的作用:

inflate方法的主要作用就是将xml转换成一个View对象,用于动态的创建布局。它的作用类似于findViewById()。

不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例化;而findViewById()是找xml布局文件下的具体widget控件(如Button、TextView等)。

2.获取LayoutInflater实例化的方法有如下三种:

1.通过SystemService获得:

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

2.通过给定的context中获得:

LayoutInflater inflater = LayoutInflater.from(context);

3.在activity中获得:

LayoutInflater inflater = getLayoutInflater();(在Activity中可以使用,实际上是View子类下window的一个函数)

这三种方式最终本质是都是调用的Context.getSystemService()。看一下该源代码:

// 1.Activity的getLayoutInflater()方法是调用PhoneWindow的getLayoutInflater()方法。
publicPhoneWindow(Contextcontext) {

super(context);

mLayoutInflater= LayoutInflater.from(context);
}
// 可以看出它其实是调用LayoutInflater.from(context)。

2.LayoutInflater.from(context):
public static LayoutInflaterfrom(Context context) {

LayoutInflaterLayoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

if(LayoutInflater== null){

thrownew AssertionError("LayoutInflaternot found.");

}
returnLayoutInflater;
}
//可以看出它其实调用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 WifiManagerWi-Fi服务

TELEPHONY_SERVICE TeleponyManager电话服务

3.inflate()方法的调用方式:

这个方法重载了四种调用方式,分别为:

1. public View inflate(int resource, ViewGroup root);

2. public View inflate(int resource, ViewGroup root, boolean attachToRoot);

3.public View inflate(
9bcd
XmlPullParser parser, ViewGroup root);

4.public View inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot);

虽然重载了四个方法,但是这四种方法最终调用的,还是第四种方式。

第四种方式也很好理解,内部实现原理就是利用Pull解析器,对Xml文件进行解析,然后返回View对象。

inflate()方法中有三个参数,分别是:

1.resource 布局的资源id

2.root 填充的根视图

3.attachToRoot 是否将载入的视图绑定到根视图中

下面例出该方法的实际应用:

首先创建layout的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="50dp"
android:background="@android:color/green"
android:gravity="center"
android:orientation="vertical">

<textview
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text"
android:textcolor="@android:color/black"
android:textsize="20sp">
</textview>
</linearlayout>Activity中的代码如下:
public class TestActivity extends Activity {
private ListView list;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);
list = (ListView) findViewById(R.id.list);
list.setAdapter(new MyAdapter(this));
}

private class MyAdapter extends BaseAdapter {

private LayoutInflater inflater;

MyAdapter(Context context) {
inflater = LayoutInflater.from(context);
}

@Override
public int getCount() {
return 20;
}

@Override
public Object getItem(int position) {
return position;
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

if (convertView == null) {
convertView = inflater.inflate(R.layout.item_list, parent,false);
}
TextView tv = (TextView) convertView.findViewById(R.id.textView);
tv.setText(position + "");
return convertView;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: