Android 中LayoutInflater的使用

來源:互聯網
上載者:User

 

大家好我們這一節講的是LayoutInflater的使用,在實際開發種LayoutInflater這個類還是非常有用的,它的作用類似於 findViewById(),

不同點是LayoutInflater是用來找layout下xml布局檔案,並且執行個體化!而findViewById()是找具體xml下的具體 widget控制項(如:Button,TextView等)。

為了讓大家容易理解我做了一個簡單的Demo,主布局main.xml裡有一個TextView和一個Button,當點擊Button,出現 Dialog,而這個Dialog的布局方式是我們在layout目錄下定義的custom_dialog.xml檔案(裡面左右分布,左邊 ImageView,右邊TextView)。

如下:

下面我將詳細的說明Demo的實現過程:

1、建立一個 Android工程,我們命名為LayoutInflaterDemo.

2、修改main.xml布局,裡面主要在 原來基礎上增加了一個Button.代碼如下:

 

view plaincopy to clipboardprint?
  1. <?xml version="1.0"   
  2. encoding="utf-8"?>  
  3. <LinearLayout   
  4. xmlns:android="http://schemas.android.com/apk/res/android"  
  5.     android:orientation="vertical"  
  6.     android:layout_width="fill_parent"  
  7.     android:layout_height="fill_parent"  
  8.     >  
  9. <TextView    
  10.     android:layout_width="fill_parent"   
  11.     android:layout_height="wrap_content"   
  12.     android:text="@string/hello"  
  13.     />  
  14. <Button  
  15.     android:id="@+id/button"  
  16.     android:layout_width="wrap_content"  
  17.     android:layout_height="wrap_content"  
  18.     android:text="ShowCustomDialog"  
  19.     />  
  20. </LinearLayout>  

 

3.定義對話方塊的布局方式,我們在layout目錄下,建立一個名為 custom_dialog.xml檔案具體代碼如下:

 

view plaincopy to clipboardprint?
  1. <?xml version="1.0"   
  2. encoding="utf-8"?>  
  3. <LinearLayout   
  4. xmlns:android="http://schemas.android.com/apk/res/android"  
  5.               android:orientation="horizontal"  
  6.               android:layout_width="fill_parent"  
  7.               android:layout_height="fill_parent"  
  8.               android:padding="10dp"  
  9.               >  
  10.     <ImageView android:id="@+id/image"  
  11.                android:layout_width="wrap_content"  
  12.                android:layout_height="fill_parent"  
  13.                android:layout_marginRight="10dp"  
  14.                />  
  15.     <TextView android:id="@+id/text"  
  16.               android:layout_width="wrap_content"  
  17.               android:layout_height="fill_parent"  
  18.               android:textColor="#FFF"  
  19.               />  
  20. </LinearLayout>  

 

4.修改主程式LayouInflaterDemo.java代碼如下:

 

view plaincopy to clipboardprint?
  1. package com.android.tutor;  
  2. import android.app.Activity;  
  3. import android.app.AlertDialog;  
  4. import android.content.Context;  
  5. import android.os.Bundle;  
  6. import android.view.LayoutInflater;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.widget.Button;  
  10. import android.widget.ImageView;  
  11. import android.widget.TextView;  
  12. public class LayoutInflaterDemo extends Activity implements   
  13. OnClickListener {  
  14.       
  15.     private Button button;  
  16.     public void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.main);  
  19.           
  20.         button = (Button)findViewById(R.id.button);  
  21.         button.setOnClickListener(this);  
  22.     }  
  23.     @Override  
  24.     public void onClick(View v) {  
  25.           
  26.         showCustomDialog();  
  27.     }  
  28.       
  29.     public void showCustomDialog()  
  30.     {  
  31.         AlertDialog.Builder builder;  
  32.         AlertDialog alertDialog;  
  33.         Context mContext = LayoutInflaterDemo.this;  
  34.           
  35.         //下面倆種方法都可以  
  36.         ////LayoutInflater inflater = getLayoutInflater();  
  37.         LayoutInflater inflater = (LayoutInflater)   
  38. mContext.getSystemService(LAYOUT_INFLATER_SERVICE);  
  39.         View layout = inflater.inflate(R.layout.custom_dialog,null);  
  40.         TextView text = (TextView) layout.findViewById(R.id.text);  
  41.         text.setText("Hello, Welcome to Mr Wei's blog!");  
  42.         ImageView image = (ImageView) layout.findViewById(R.id.image);  
  43.         image.setImageResource(R.drawable.icon);  
  44.         builder = new AlertDialog.Builder(mContext);  
  45.         builder.setView(layout);  
  46.         alertDialog = builder.create();  
  47.         alertDialog.show();  
  48.     }  
  49. }  

 

5、最後執行之,點擊Button,將得到上述效果。

 好 今天就到此為止,睡覺了,大家有什麼不明白的請留言~謝謝!

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.