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

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(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;
	        }
	    }
	}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值