LayoutInflater的使用:動態加載佈局

LayoutInflater的作用和findViewById()差不多,不同之處在於LayoutInflater是找layout文件夾下的某個xml佈局文件!而 findViewById()是找某一個xml下的具體 widget控件。

LayoutInflater可以將一個xml佈局文件轉化爲View 類型,其代碼如下:

View Code
 package hzx.one;
                        
 import android.app.Activity;
 import android.app.AlertDialog;
 import android.app.AlertDialog.Builder;
 import android.content.DialogInterface;
 import android.os.Bundle;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.Button;
 import android.widget.Toast;
                        
 public class HzxDialogActivity extends Activity {
     private Button button1;
     private Button button2;
     private Button button3;
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);
         button1=(Button) findViewById(R.id.button1);
         button1.setText("默認dialog");
         button1.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {showDialog1();}});
         button2=(Button) findViewById(R.id.button2);
         button2.setText("自定義dialog");
         button2.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {showDialog2();}});
         button3=(Button) findViewById(R.id.button3);
         button3.setText("自定義toast");
         button3.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 Toast toast=new Toast(HzxDialogActivity.this);
                 //三種方法定義LayoutInflater
                 //LayoutInflater li=getLayoutInflater();
                 //LayoutInflater li = LayoutInflater.from(HzxDialogActivity.this);
                 LayoutInflater li = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
                                        
                 View view=li.inflate(R.layout.dialog, (ViewGroup)findViewById(R.id.dialog));
                 toast.setView(view);
                 toast.setDuration(Toast.LENGTH_SHORT);
                 toast.setGravity(0, -20, 20);
                 toast.show();
             }});
     }
     private void showDialog1() {
         AlertDialog.Builder builder=new Builder(this);
         builder.setMessage("are you sure to exit ?");
         builder.setTitle("exit or not ");
         builder.setPositiveButton("yes", new DialogInterface.OnClickListener() {
             @Override
             public void onClick(DialogInterface dialog, int which) {dialog.dismiss();HzxDialogActivity.this.finish();}});
         builder.setNegativeButton("no", new DialogInterface.OnClickListener() {
             @Override
             public void onClick(DialogInterface dialog, int which) {dialog.dismiss();}});
         builder.create().show();
     }
     private void showDialog2(){
         AlertDialog.Builder builder=new Builder(this);
         LayoutInflater li=getLayoutInflater();
         View v=li.inflate(R.layout.dialog, (ViewGroup)findViewById(R.id.dialog));
         builder.setView(v);
         builder.setPositiveButton("yes", new DialogInterface.OnClickListener() {
             @Override
             public void onClick(DialogInterface dialog, int which) {dialog.dismiss();HzxDialogActivity.this.finish();}});
         builder.setNegativeButton("no", new DialogInterface.OnClickListener() {
             @Override
             public void onClick(DialogInterface dialog, int which) {dialog.dismiss();}});
         builder.create().show();
     }
 }


main.xml佈局文件

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent" android:orientation="vertical">
 <TextView 
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:text="@string/hello"
     />
 <Button android:text="Button" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
 <Button android:text="Button" android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
 <Button android:text="Button" android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
 </LinearLayout>


dialog.xml佈局文件:

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:orientation="horizontal"
     android:id="@+id/dialog">
                
     <ImageView android:id="@+id/imageView1" android:layout_height="wrap_content" android:src="@drawable/icon" android:layout_width="wrap_content"></ImageView>
     <TextView android:layout_height="wrap_content" android:text="@string/hello" android:layout_width="wrap_content" android:id="@+id/textView1"></TextView>
 </LinearLayout>


演示結果如下:

650) this.width=650;" src="http://pic002.cnblogs.com/images/2011/328051/2011092210295494.png" style="margin:0px;padding:0px;border:0px;" />

默認dialog:

650) this.width=650;" src="http://pic002.cnblogs.com/images/2011/328051/2011092210302937.png" style="margin:0px;padding:0px;border:0px;" />

自定義dialog:

650) this.width=650;" src="http://pic002.cnblogs.com/images/2011/328051/2011092210310180.png" style="margin:0px;padding:0px;border:0px;" />

自定義toast:

650) this.width=650;" src="http://pic002.cnblogs.com/images/2011/328051/2011092210312793.png" style="margin:0px;padding:0px;border:0px;" />


參考:http://www.cnblogs.com/zhukoo/archive/2011/09/22/2184798.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章