大家好我們這一節講的是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.代碼如下:
?
~~~
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns: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>
~~~
?
3.定義對話框的布局方式,我們在layout目錄下,新建一個名為custom_dialog.xml文件具體代碼如下:
?
~~~
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns: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>
~~~
?
4.修改主程序LayouInflaterDemo.java代碼如下:
?
~~~
package com.android.tutor;import android.app.Activity;import android.app.AlertDialog;import android.content.Context;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ImageView;import android.widget.TextView;public class LayoutInflaterDemo extends Activity implements OnClickListener { private Button button; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); button = (Button)findViewById(R.id.button); button.setOnClickListener(this); } @Override public void onClick(View v) { showCustomDialog(); } public void showCustomDialog() { AlertDialog.Builder builder; AlertDialog alertDialog; Context mContext = LayoutInflaterDemo.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(); }}
~~~
5、最后執行之,點擊Button,將得到上述效果。
?
?好今天就到此為止,睡覺了,大家有什么不明白的請留言~謝謝!
- 前言
- (一)Android常用名令集錦(圖文并茂)!
- (二)Android Launcher抽屜類SlidingDrawer的使用!
- (三)Android 中自定義View的應用.
- (四)Android 中自定義屬性(attr.xml,TypedArray)的使用!
- (五)Android 中LayoutInflater的使用!
- (六)Android 中MenuInflater的使用(布局定義菜單)!
- (七)Android 中Preferences的使用!
- (八)Android Widget開發案例(世界杯倒計時!)
- (九)Android Handler的使用!!!
- (十)Android PopupWindow的使用!!!
- (十一)Android 通用獲取Ip的方法(判斷手機是否聯網的方法)!!!
- (十二)Android 在一個應用中如何啟動另外一個已安裝的應用!!!
- (十三)Android 數據庫SQLiteDatabase的使用!!
- (十四)Android Location的使用!!
- (十五)通過Location獲取Address的使用!
- (十六)Android中萬能的BaseAdapter(Spinner,ListView,GridView)的使用!
- Android 中的拿來主義(編譯,反編譯,AXMLPrinter2,smali,baksmali)!
- (十七)Android中Intent傳遞對象的兩種方法(Serializable,Parcelable)!
- (十八)列出Android設備中所有啟動的服務,及判斷某個服務是否開啟!
- (十九)Android開發中,使用線程應該注意的問題!
- (二十)Android與JavaScript方法相互調用!
- (二十一)Android中創建與幾種解析xml的方法!
- (二十二)Android中幾種圖像特效處理的集錦!!
- (二十三)Android中的日歷讀寫操作!!!
- (二十四)Android WebView的緩存!!!
- (二十五)Android 中的AIDL!!!