大家好我們這一節講的是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?
- <?
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
>
- <?
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文件具體代碼如下:
- view plaincopy to clipboardprint?
- <?
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
>
- <?
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代碼如下:
- view plaincopy to clipboardprint?
- 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 = (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();
- }
- }
- 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 = (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,將得到上述效果。
好今天就到此爲止,睡覺了,大家有什麼不明白的請留言~謝謝!