LayoutInflater详解

 分析时候遇到的,记录一个java




做用: 
一、对于一个没有被载入或者想要动态载入的界面, 都须要使用inflate来载入. 

二、对于一个已经载入的Activity, 就可使用实现了这个Activiyt的的findViewById方法来得到其中的界面元素. 

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

   其实在Activity里面就使用了LayoutInflater来载入界面, 经过getSystemService(Context.LAYOUT_INFLATER_SERVICE)方法能够得到一个 LayoutInflater, 也能够经过LayoutInflater inflater = getLayoutInflater();来得到.而后使用inflate方法来载入layout的xml, 
android



首先咱们要知道,什么是已经被载入的layout,什么是尚未载入的.咱们启动一个应用,与入口Activity相关的layout{常见的是main.xml}就是被载入的,即在Oncreate()中的.而其余的layout是没有被载入的.就要动态载入了或经过另外一个activity.app


在实际开发种LayoutInflater这个类仍是很是有用的,它的做用相似于 findViewById(),ide

不一样点是LayoutInflater是用来找layout下xml布局文件,而且实例化!而findViewById()是找具体xml下的具体 widget控件.布局

为了让你们容易理解我[转]作了一个简单的Demo,主布局main.xml里有一个TextView和一个Button,当点击Button,出现 Dialog,而这个Dialog的布局方式是咱们在layout目录下定义的custom_dialog.xml文件(里面左右分布,左边 ImageView,右边TextView)。ui



在实际开发种LayoutInflater这个类仍是很是有用的,它的做用相似于findViewById(),不一样点是LayoutInflater是用来找layout下xml布局文件,而且实例化!而findViewById()是找具体xml下的具体widget控件(如:Button,TextView等)。this

  主布局main.xml里有一个TextView和一个Button,当点击Button,出现Dialog,而这个Dialog的布局方式是咱们在layout目录下定义的custom_dialog.xml文件(里面左右分布,左边ImageView,右边TextView)。spa

  

  



XML代码:orm


  <?xmlversion="1.0"encoding="utf-8"?>xml

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

  AlertDialogalertDialog;

  ContextmContext=LayoutInflaterDemo.this;

  

  //下面俩种方法均可以

  ////LayoutInflaterinflater=getLayoutInflater();

  

  LayoutInflaterinflater=(LayoutInflater)mContext.getSystemServic(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();

  }

  

  }  

相关文章
相关标签/搜索