原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。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. 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,将得到上述效果。
 好今天就到此为止,睡觉了,大家有什么不明白的请留言~谢谢!
 
from:http://weizhulin.blog.51cto.com/1556324/311450/

Android高手进阶教程(五)之----Android 中LayoutInflater的使用!的更多相关文章

  1. Android高手进阶教程(十七)之---Android中Intent传递对象的两种方法(Serializable,Parcelable)!

    [转][原文] 大家好,好久不见,今天要给大家讲一下Android中Intent中如何传递对象,就我目前所知道的有两种方法,一种是Bundle.putSerializable(Key,Object); ...

  2. Android高手进阶教程(七)之----Android 中Preferences的使用!

    http://blog.csdn.net/Android_Tutor/article/details/5531849 大家好,我们这一节讲的是Android Preferences 的学习,Prefe ...

  3. Android高手进阶教程(二十八)之---Android ViewPager控件的使用(基于ViewPager的横向相册)!!!

      分类: Android高手进阶 Android基础教程 2012-09-14 18:10 29759人阅读 评论(35) 收藏 举报 android相册layoutobjectclassloade ...

  4. Android Studio系列教程五--Gradle命令详解与导入第三方包

    Android Studio系列教程五--Gradle命令详解与导入第三方包 2015 年 01 月 05 日 DevTools 本文为个人原创,欢迎转载,但请务必在明显位置注明出处!http://s ...

  5. Android 高手进阶之自定义View,自定义属性(带进度的圆形进度条)

      Android 高手进阶(21)  版权声明:本文为博主原创文章,未经博主允许不得转载. 转载请注明地址:http://blog.csdn.net/xiaanming/article/detail ...

  6. Android高手进阶:Adapter深入理解与优化

    一般是针对包含多个元素的View,如ListView,GridView,ExpandableListview,的时候我们是给其设置一个Adapter.Adapter是与View之间提供数据的桥梁,也是 ...

  7. Android高手进阶——Adapter深入理解与优化

    Android高手进阶--Adapter深入理解与优化 通常是针对包括多个元素的View,如ListView,GridView.ExpandableListview,的时候我们是给其设置一个Adapt ...

  8. SpringBoot进阶教程(五十九)整合Codis

    上一篇博文<详解Codis安装与部署>中,详细介绍了codis的安装与部署,这篇文章主要介绍介绍springboot整合codis.如果之前看过<SpringBoot进阶教程(五十二 ...

  9. Android高手进阶篇4-实现侧滑菜单框架,一分钟集成到项目中

    先来看下面的这张效果图: 上面这张效果图是百度影音的,现在在Android上很流行,最初是由facebook自己实现的,而后各大应用有跟风之势,那么这种侧滑效果是如何实现的呢? 网上现在这种侧滑菜单的 ...

随机推荐

  1. mq_close

    NAME mq_close - 关闭一个消息队列 (REALTIME) SYNOPSIS #include <mqueue.h>int mq_close(mqd_t mqdes) DESC ...

  2. AppWidget框架

    AppWidget 框架主要包括以下类: 1.AppWidgetProvider:继承自BroadcastReceiver,在AppWidget应用update.enable.disable和dele ...

  3. iOS开发--泛型

    一. 泛型函数 1.单一占位符泛型函数 下面就使用一个经典案例:两个数值进行交换.来使用泛型,写一个通用的函数,这个函数的功能就是交换两个变量的值.在Swift中不允许类型隐式转换,也就是说,如果你定 ...

  4. Ibm-jQuery教程学习笔记

    一.概述 1.虽然 jQuery 本身并非一门新的语言.但是,学习其语法有助于我们熟练.灵活地使用它.回顾下我们熟悉的 CSS 语法,不难发现 jQuery 的语法与 CSS 有相似之处. jQuer ...

  5. 一个简单的以User权限启动外部应用程序(用NetUserAdd函数和USER_INFO_1结构体动态添加用户,然后用CreateProcessWithLogonW启动程序)

    版权声明:本文为博主原创文章,未经博主允许不得转载. BOOL ExecuteAsUser(LPCWSTR lpszUserName, LPCWSTR lpszPassword, LPCWSTR lp ...

  6. Struts2笔记——struts常用标签

    使用struts标签前,首先要配置struts2架构,然后导入标签库,jsp插入如下语句: <%@taglib uri="/struts-tags" prefix=" ...

  7. 【web性能】让css更简洁、高效

    用了这么多年的CSS,现在才明白CSS的真正匹配原理,不知道你是否也跟我一样?   看1个简单的CSS: DIV#divBox p span.red{color:red;},按习惯我们对这个CSS 的 ...

  8. C 语言文件操作

    C 语言文件操作 1. 数据流:     程序与数据的交互以流的形式进行.fopen 即打开数据流,fclose 即刷新数据流.     所谓数据流,是一种抽象,表示这段数据像流一样,需要逐步接收,不 ...

  9. Android百度地图开发05之公交信息检索 + 路线规划

    在上一篇blog中介绍过POI检索的使用,本篇blog主要介绍公交信息检索和线路规划的内容. 公交信息检索 实际上,公交信息检索与POI检索.在线建议检索非常相似,也是把你需要检索的信息发送给百度地图 ...

  10. CentOS7安装配置Apache HTTP Server

    RPM安装httpd 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 # yum -yinstall http ...