LayoutInflater详解

Android里面想要创建一个画面的时候, 初学一般都是新建一个类, 继承Activity基类, 然后在onCreate里面使用setContentView方法来载入一个在xml里定义好的界面.

其实在Activity里面就使用了LayoutInflater来载入界面, 通过getSystemService(Context.LAYOUT_INFLATER_SERVICE)方法可以获得一个LayoutInflater, 然后使用inflate方法来载入layout的xml, 对于一个没有被载入或者想要动态载入的界面, 都需要使用inflate来载入.

对于一个已经载入的界面, 就可以使用这个界面调用findViewById方法来获得其中的子界面了.

 

 

 

 

在实际开发种LayoutInflater这个类还是非常有用的,它的作用类似于findViewById(),不同点是LayoutInflater是用来找layout下xml布局文件,并且实例化!而findViewById()是找具体xml下的具体widget控件(如:Button,TextView等)。
  主布局main.xml里有一个TextView和一个Button,当点击Button,出现Dialog,而这个Dialog的布局方式是我们在layout目录下定义的custom_dialog.xml文件(里面左右分布,左边ImageView,右边TextView)。
  
  

LayoutInflater详解 - 十字心 - 十字心 的博客

XML代码:

  <?xmlversion="1.0"encoding="utf-8"?>
  <LinearLayoutxmlns: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"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="ShowCustomDialog"/>
  
  </LinearLayout>

  定义对话框的布局方式,我们在layout目录下,新建一个名为custom_dialog.xml文件具体代码如下:
  

XML代码:

  <?xmlversion="1.0"encoding="utf-8"?>
  <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="horizontal"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:padding="10dp">
  
  <ImageView
  android:id="@+id/image"
  android:layout_width="wrap_content"
  android:layout_height="fill_parent"
  android:layout_marginRight="10dp"/>
  
  <TextView
  android:id="@+id/text"
  android:layout_width="wrap_content"
  android:layout_height="fill_parent"
  android:textColor="#FFF"/>
  
  </LinearLayout>

  主程序LayouInflaterDemo.java代码如下:
  
    packageEOE.android.Demo;
  importandroid.app.Activity;
  importandroid.app.AlertDialog;
  importandroid.content.Context;
  importandroid.os.Bundle;
  importandroid.view.LayoutInflater;
  importandroid.view.View;
  importandroid.view.View.OnClickListener;
  importandroid.widget.Button;
  importandroid.widget.ImageView;
  importandroid.widget.TextView;
  
  publicclassLayoutInflaterDemoextendsActivityimplementsOnClickListener{
  privateButtonbutton;
  
  publicvoidonCreate(BundlesavedInstanceState){
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  button=(Button)findViewById(R.id.button);
  button.setOnClickListener(this);
  }
  
  @Override
  publicvoidonClick(Viewv){
  showCustomDialog();
  }
  publicvoidshowCustomDialog(){
  AlertDialog.Builderbuilder;
  AlertDialog alertDialog;
  Context mContext=LayoutInflaterDemo.this;
  
  //下面俩种方法都可以
  LayoutInflater inflater=getLayoutInflater();
  
  LayoutInflater inflater=(LayoutInflater)mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
  
  Viewlayout=inflater.inflate(R.layout.custom_dialog,null);
  TextViewtext=(TextView)layout.findViewById(R.id.text);
  text.setText("Hello,WelcometoMrWei'sblog!");
  ImageViewimage=(ImageView)layout.findViewById(R.id.image);
  image.setImageResource(R.drawable.icon);
  builder=newAlertDialog.Builder(mContext);
  builder.setView(layout);
  alertDialog=builder.create();
  alertDialog.show();
  }
  
  }  


Inflater英文意思是膨胀,在 Android 中应该是扩展的意思吧。 
LayoutInflater的作用类似于 findViewById(),不同点是LayoutInflater是用来找layout文件夹下的xml布局文件,并且实例化!而 findViewById()是找具体某一个xml下的具体 widget控件(如:Button,TextView等)。

(0)她可以有很多地方可以使用,如BaseAdapter的getView中,自定义Dialog中取得view中的组件widget等等。
它的用法有2种:


复制到剪贴板 Java代码
view plaincopy to clipboardprint? 
LayoutInflater inflater = LayoutInflater.from(this); 
View view=inflater.inflate(R.layout.ID, null); 
或者干脆并成一句: 
View view=LayoutInflater.from(this).inflate(R.layout.ID, null); 
另一种方法: 
复制到剪贴板 Java代码
view plaincopy to clipboardprint? 
LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE); 
View view=inflater.inflate(R.layout.ID, null); 
上面2种方法本质上是一样的,看下面的源码,form()调用的就是getSystemService(): 
复制到剪贴板 Java代码
Java代码 
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; 

另外getSystemService()是Android很重要的一个API,它是Activity的一个方法,根据传入的NAME来取得对应的Object,然后转换成相应的服务对象。以下介绍系统相应的服务。 
传入的Name返回的对象说明
WINDOW_SERVICEWindowManager管理打开的窗口程序
LAYOUT_INFLATER_SERVICELayoutInflater取得xml里定义的view
ACTIVITY_SERVICEActivityManager管理应用程序的系统状态
POWER_SERVICEPowerManger电源的服务
ALARM_SERVICEAlarmManager闹钟的服务
NOTIFICATION_SERVICENotificationManager状态栏的服务
KEYGUARD_SERVICEKeyguardManager键盘锁的服务
LOCATION_SERVICELocationManager位置的服务,如GPS
SEARCH_SERVICESearchManager搜索的服务
VEBRATOR_SERVICEVebrator手机震动的服务
CONNECTIVITY_SERVICEConnectivity网络连接的服务
WIFI_SERVICEWifiManagerWi-Fi服务
TELEPHONY_SERVICETeleponyManager电话服务


复制到剪贴板 Java代码
Java代码 
//基本用法 
public void showCustomDialog(){ 
AlertDialog.Builder builder; 
AlertDialog alertDialog; 
Context mContext = AppActivity.this; 
//下面俩种方法都可以 
//LayoutInflater inflater = getLayoutInflater(); 
LayoutInflater inflater = (LayoutInflater) 
mContext.getSystemService(LAYOUT_INFLATER_SERVICE); 
View layout = inflater.inflate(R.layout.custom_dialog,null); 
TextView text = (TextView) layout.findViewById(R.id.text); 
text.setText("Hello, Welcome to Mr Wei's blog!"); 
ImageView image = (ImageView) layout.findViewById(R.id.image); 
image.setImageResource(R.drawable.icon); 
builder = new AlertDialog.Builder(mContext); 
builder.setView(layout); 
alertDialog = builder.create(); 
alertDialog.show(); 


protected void showToast(int type) { 
Toast.makeText(this, "*********", Toast.LENGTH_LONG).show(); 
LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
View view = li.inflate(R.layout.toast, null); 
Toast toast = new Toast(this); 
toast.setView(view); 
toast.setDuration(type); 
toast.show(); 
}

 

 

LayoutInflaterAPI的介绍中可以知道,该类是主要用于对XML文件的转化成一个响应的视图对象的类。应用该功能之前还需声明一个LayoutInflater工厂对象。有三种方法可以使用:

1、LayoutInflater factory = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

2、LayoutInflater factory = (LayoutInflater)LayoutInflater.from(context);

3、LayoutInflater factory = (LayoutInflater)context.getLayoutInflater();

其中:context是对应的Activity类名。

方法2中调用了方法1

方法3中不太清楚实现细节。知道的可以赐教。

接下来可以使用LayoutInflater对象的inflater的各种不同的方法来生产XML相应的对象。

在下面就是把该XML对象加入到响应的容器当中。可以是AlterDialog.Builder当中。调用该容器的setView方法即可。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值