大家好我们这一节讲的是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.代码如下:
  1. view plaincopy to clipboardprint?
  2. <?xml version="1.0"
  3. encoding="utf-8"?>
  4. <LinearLayout
  5. xmlns:android="http://schemas.android.com/apk/res/android"
  6. android:orientation="vertical"
  7. android:layout_width="fill_parent"
  8. android:layout_height="fill_parent"
  9. >
  10. <TextView
  11. android:layout_width="fill_parent"
  12. android:layout_height="wrap_content"
  13. android:text="@string/hello"
  14. />
  15. <Button
  16. android:id="@+id/button"
  17. android:layout_width="wrap_content"
  18. android:layout_height="wrap_content"
  19. android:text="ShowCustomDialog"
  20. />
  21. </LinearLayout>
  22. <?xml version="1.0"
  23. encoding="utf-8"?>
  24. <LinearLayout
  25. xmlns:android="http://schemas.android.com/apk/res/android"
  26. android:orientation="vertical"
  27. android:layout_width="fill_parent"
  28. android:layout_height="fill_parent"
  29. >
  30. <TextView
  31. android:layout_width="fill_parent"
  32. android:layout_height="wrap_content"
  33. android:text="@string/hello"
  34. />
  35. <Button
  36. android:id="@+id/button"
  37. android:layout_width="wrap_content"
  38. android:layout_height="wrap_content"
  39. android:text="ShowCustomDialog"
  40. />
  41. </LinearLayout>
3.定义对话框的布局方式,我们在layout目录下,新建一个名为 custom_dialog.xml文件具体代码如下:
  1. view plaincopy to clipboardprint?
  2. <?xml version="1.0"
  3. encoding="utf-8"?>
  4. <LinearLayout
  5. xmlns:android="http://schemas.android.com/apk/res/android"
  6. android:orientation="horizontal"
  7. android:layout_width="fill_parent"
  8. android:layout_height="fill_parent"
  9. android:padding="10dp"
  10. >
  11. <ImageView android:id="@+id/image"
  12. android:layout_width="wrap_content"
  13. android:layout_height="fill_parent"
  14. android:layout_marginRight="10dp"
  15. />
  16. <TextView android:id="@+id/text"
  17. android:layout_width="wrap_content"
  18. android:layout_height="fill_parent"
  19. android:textColor="#FFF"
  20. />
  21. </LinearLayout>
  22. <?xml version="1.0"
  23. encoding="utf-8"?>
  24. <LinearLayout
  25. xmlns:android="http://schemas.android.com/apk/res/android"
  26. android:orientation="horizontal"
  27. android:layout_width="fill_parent"
  28. android:layout_height="fill_parent"
  29. android:padding="10dp"
  30. >
  31. <ImageView android:id="@+id/image"
  32. android:layout_width="wrap_content"
  33. android:layout_height="fill_parent"
  34. android:layout_marginRight="10dp"
  35. />
  36. <TextView android:id="@+id/text"
  37. android:layout_width="wrap_content"
  38. android:layout_height="fill_parent"
  39. android:textColor="#FFF"
  40. />
  41. </LinearLayout>
4.修改主程序LayouInflaterDemo.java代码如下:
  1. view plaincopy to clipboardprint?
  2. package com.android.tutor;
  3. import android.app.Activity;
  4. import android.app.AlertDialog;
  5. import android.content.Context;
  6. import android.os.Bundle;
  7. import android.view.LayoutInflater;
  8. import android.view.View;
  9. import android.view.View.OnClickListener;
  10. import android.widget.Button;
  11. import android.widget.ImageView;
  12. import android.widget.TextView;
  13. public class LayoutInflaterDemo extends Activity implements
  14. OnClickListener {
  15. private Button button;
  16. public void onCreate(Bundle savedInstanceState) {
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.main);
  19. button = (Button)findViewById(R.id.button);
  20. button.setOnClickListener(this);
  21. }
  22. @Override
  23. public void onClick(View v) {
  24. showCustomDialog();
  25. }
  26. public void showCustomDialog()
  27. {
  28. AlertDialog.Builder builder;
  29. AlertDialog alertDialog;
  30. Context mContext = LayoutInflaterDemo.this;
  31. //下面俩种方法都可以
  32. ////LayoutInflater inflater = getLayoutInflater();
  33. LayoutInflater inflater = (LayoutInflater)
  34. mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
  35. View layout = inflater.inflate(R.layout.custom_dialog,null);
  36. TextView text = (TextView) layout.findViewById(R.id.text);
  37. text.setText("Hello, Welcome to Mr Wei's blog!");
  38. ImageView image = (ImageView) layout.findViewById(R.id.image);
  39. image.setImageResource(R.drawable.icon);
  40. builder = new AlertDialog.Builder(mContext);
  41. builder.setView(layout);
  42. alertDialog = builder.create();
  43. alertDialog.show();
  44. }
  45. }
  46. package com.android.tutor;
  47. import android.app.Activity;
  48. import android.app.AlertDialog;
  49. import android.content.Context;
  50. import android.os.Bundle;
  51. import android.view.LayoutInflater;
  52. import android.view.View;
  53. import android.view.View.OnClickListener;
  54. import android.widget.Button;
  55. import android.widget.ImageView;
  56. import android.widget.TextView;
  57. public class LayoutInflaterDemo extends Activity implements
  58. OnClickListener {
  59. private Button button;
  60. public void onCreate(Bundle savedInstanceState) {
  61. super.onCreate(savedInstanceState);
  62. setContentView(R.layout.main);
  63. button = (Button)findViewById(R.id.button);
  64. button.setOnClickListener(this);
  65. }
  66. @Override
  67. public void onClick(View v) {
  68. showCustomDialog();
  69. }
  70. public void showCustomDialog()
  71. {
  72. AlertDialog.Builder builder;
  73. AlertDialog alertDialog;
  74. Context mContext = LayoutInflaterDemo.this;
  75. //下面俩种方法都可以
  76. ////LayoutInflater inflater = getLayoutInflater();
  77. LayoutInflater inflater = (LayoutInflater)
  78. mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
  79. View layout = inflater.inflate(R.layout.custom_dialog,null);
  80. TextView text = (TextView) layout.findViewById(R.id.text);
  81. text.setText("Hello, Welcome to Mr Wei's blog!");
  82. ImageView image = (ImageView) layout.findViewById(R.id.image);
  83. image.setImageResource(R.drawable.icon);
  84. builder = new AlertDialog.Builder(mContext);
  85. builder.setView(layout);
  86. alertDialog = builder.create();
  87. alertDialog.show();
  88. }
  89. }
5、最后执行之,点击Button,将得到上述效果。
posted on 2011-08-18 22:01  陈孝勇  阅读(240)  评论(0编辑  收藏  举报