原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。 http://weizhulin.blog.51cto.com/1556324/311450 大家好我们这一节讲的是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. viewplaincopytoclipboardprint?
  2. <?xmlversion="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. <?xmlversion="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. viewplaincopytoclipboardprint?
  2. <?xmlversion="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. <ImageViewandroid:id="@+id/image"
  12. android:layout_width="wrap_content"
  13. android:layout_height="fill_parent"
  14. android:layout_marginRight="10dp"
  15. />
  16. <TextViewandroid:id="@+id/text"
  17. android:layout_width="wrap_content"
  18. android:layout_height="fill_parent"
  19. android:textColor="#FFF"
  20. />
  21. </LinearLayout>
  22. <?xmlversion="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. <ImageViewandroid:id="@+id/image"
  32. android:layout_width="wrap_content"
  33. android:layout_height="fill_parent"
  34. android:layout_marginRight="10dp"
  35. />
  36. <TextViewandroid: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. viewplaincopytoclipboardprint?
  2. packagecom.android.tutor;
  3. importandroid.app.Activity;
  4. importandroid.app.AlertDialog;
  5. importandroid.content.Context;
  6. importandroid.os.Bundle;
  7. importandroid.view.LayoutInflater;
  8. importandroid.view.View;
  9. importandroid.view.View.OnClickListener;
  10. importandroid.widget.Button;
  11. importandroid.widget.ImageView;
  12. importandroid.widget.TextView;
  13. publicclassLayoutInflaterDemoextendsActivityimplements
  14. OnClickListener{
  15. privateButtonbutton;
  16. publicvoidonCreate(BundlesavedInstanceState){
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.main);
  19. button=(Button)findViewById(R.id.button);
  20. button.setOnClickListener(this);
  21. }
  22. @Override
  23. publicvoidonClick(Viewv){
  24. showCustomDialog();
  25. }
  26. publicvoidshowCustomDialog()
  27. {
  28. AlertDialog.Builderbuilder;
  29. AlertDialogalertDialog;
  30. ContextmContext=LayoutInflaterDemo.this;
  31. //下面俩种方法都可以
  32. ////LayoutInflaterinflater=getLayoutInflater();
  33. LayoutInflaterinflater=(LayoutInflater)
  34. mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
  35. Viewlayout=inflater.inflate(R.layout.custom_dialog,null);
  36. TextViewtext=(TextView)layout.findViewById(R.id.text);
  37. text.setText("Hello,WelcometoMrWei'sblog!");
  38. ImageViewimage=(ImageView)layout.findViewById(R.id.image);
  39. image.setImageResource(R.drawable.icon);
  40. builder=newAlertDialog.Builder(mContext);
  41. builder.setView(layout);
  42. alertDialog=builder.create();
  43. alertDialog.show();
  44. }
  45. }
  46. packagecom.android.tutor;
  47. importandroid.app.Activity;
  48. importandroid.app.AlertDialog;
  49. importandroid.content.Context;
  50. importandroid.os.Bundle;
  51. importandroid.view.LayoutInflater;
  52. importandroid.view.View;
  53. importandroid.view.View.OnClickListener;
  54. importandroid.widget.Button;
  55. importandroid.widget.ImageView;
  56. importandroid.widget.TextView;
  57. publicclassLayoutInflaterDemoextendsActivityimplements
  58. OnClickListener{
  59. privateButtonbutton;
  60. publicvoidonCreate(BundlesavedInstanceState){
  61. super.onCreate(savedInstanceState);
  62. setContentView(R.layout.main);
  63. button=(Button)findViewById(R.id.button);
  64. button.setOnClickListener(this);
  65. }
  66. @Override
  67. publicvoidonClick(Viewv){
  68. showCustomDialog();
  69. }
  70. publicvoidshowCustomDialog()
  71. {
  72. AlertDialog.Builderbuilder;
  73. AlertDialogalertDialog;
  74. ContextmContext=LayoutInflaterDemo.this;
  75. //下面俩种方法都可以
  76. ////LayoutInflaterinflater=getLayoutInflater();
  77. LayoutInflaterinflater=(LayoutInflater)
  78. mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
  79. Viewlayout=inflater.inflate(R.layout.custom_dialog,null);
  80. TextViewtext=(TextView)layout.findViewById(R.id.text);
  81. text.setText("Hello,WelcometoMrWei'sblog!");
  82. ImageViewimage=(ImageView)layout.findViewById(R.id.image);
  83. image.setImageResource(R.drawable.icon);
  84. builder=newAlertDialog.Builder(mContext);
  85. builder.setView(layout);
  86. alertDialog=builder.create();
  87. alertDialog.show();
  88. }
  89. }
5、最后执行之,点击Button,将得到上述效果。 好今天就到此为止,睡觉了,大家有什么不明白的请留言~谢谢!

本文出自 “Android_Tutor” 博客,请务必保留此出处http://weizhulin.blog.51cto.com/1556324/311450

更多相关文章

  1. 加速Android(安卓)Studio/Gradle构建
  2. Android(安卓)离线安装宝典
  3. 家庭版记账本app进度之关于android界面布局的相关学习
  4. Android(安卓)聊天界面对话
  5. Android夜间模式实现,通过在window上加一层半透明的View
  6. launcher修改--获取屏幕缩略(预览)图
  7. 关于Android中传递数据的一些讨论
  8. [Google Android] Creating Your Own Spelling Checker Service
  9. android 网络图片双缓存

随机推荐

  1. Android悬浮窗口
  2. Android(安卓)getMeasuredHeight()与getH
  3. 第一行代码:AlertDialog
  4. Android(安卓)Development Notes -1
  5. android ndk 纵览
  6. android 菜单事件处理
  7. android 网络判断
  8. Android(安卓)- LayoutInflater 的使用
  9. android tv-TV Apps Checklist
  10. android:fitsSystemWindows=“true”