- 浏览: 137119 次
- 性别:
文章分类
- 全部博客 (226)
- Android (181)
- C# (8)
- BOOTSTRAP (1)
- ASP.NET MVC4 (1)
- 设计模式 (1)
- VB.NET (1)
- WPF (0)
- PLC (0)
- 电气图纸 (0)
- 数据库 (5)
- Java (5)
- Window phone (0)
- 仪器仪表 (0)
- 变频器 (0)
- 低压电器 (0)
- 物联网 (0)
- Photoshop (1)
- SVN (1)
- 单片机 (5)
- IT (1)
- Android_IOS风格 (0)
- Android_广告栏展示 (0)
- Android_动画 (1)
- Android_Adapter (0)
- Android_ListView (1)
- Android_File (2)
- Android_表单提交 (0)
- Android_WebView (1)
- PHP (2)
- Android_Excel (1)
- Android_drawable (1)
- Android_theme (2)
- Android_phonegap (2)
- Android_AndroidManifest (1)
- ThinkPHP (0)
- Jquery (1)
- Android_ContentProvider (1)
最新评论
在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById()。不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例化;而findViewById()是找xml布局文件下的具体widget控件(如Button、TextView等)。
具体作用:
1、对于一个没有被载入或者想要动态载入的界面,都需要使用LayoutInflater.inflate()来载入;
2、对于一个已经载入的界面,就可以使用Activiyt.findViewById()方法来获得其中的界面元素。
LayoutInflater 是一个抽象类,在文档中如下声明:
public abstract class LayoutInflater extends Object
获得 LayoutInflater 实例的三种方式
1. LayoutInflater inflater = getLayoutInflater();//调用Activity的getLayoutInflater()
2. LayoutInflater inflater = LayoutInflater.from(context);
3. LayoutInflater inflater = (LayoutInflater)context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
其实,这三种方式本质是相同的,从源码中可以看出:
getLayoutInflater():
Activity 的 getLayoutInflater() 方法是调用 PhoneWindow 的getLayoutInflater()方法,看一下该源代码:
public PhoneWindow(Context context)
{
super(context);
mLayoutInflater = LayoutInflater.from(context);
}
可以看出它其实是调用 LayoutInflater.from(context)。
LayoutInflater.from(context):
public static LayoutInflater from(Context context)
{
LayoutInflater LayoutInflater = (LayoutInflater) context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
if (LayoutInflater == null)
{
throw new AssertionError("LayoutInflater not found.");
}
return LayoutInflater;
}
可以看出它其实调用 context.getSystemService()。
结论:所以这三种方式最终本质是都是调用的Context.getSystemService()。
另外getSystemService()是Android很重要的一个API,它是Activity的一个方法,根据传入的NAME来取得对应的Object,然后转换成相应的服务对象。以下介绍系统相应的服务。
传入的Name 返回的对象 说明
WINDOW_SERVICE WindowManager 管理打开的窗口程序
LAYOUT_INFLATER_SERVICE LayoutInflater 取得xml里定义的view
ACTIVITY_SERVICE ActivityManager 管理应用程序的系统状态
POWER_SERVICE PowerManger 电源的服务
ALARM_SERVICE AlarmManager 闹钟的服务
NOTIFICATION_SERVICE NotificationManager 状态栏的服务
KEYGUARD_SERVICE KeyguardManager 键盘锁的服务
LOCATION_SERVICE LocationManager 位置的服务,如GPS
SEARCH_SERVICE SearchManager 搜索的服务
VEBRATOR_SERVICE Vebrator 手机震动的服务
CONNECTIVITY_SERVICE Connectivity 网络连接的服务
WIFI_SERVICE WifiManager Wi-Fi服务
TELEPHONY_SERVICE TeleponyManager 电话服务
inflate 方法
通过 sdk 的 api 文档,可以知道该方法有以下几种过载形式,返回值均是 View 对象,如下:
public View inflate (int resource, ViewGroup root)
public View inflate (XmlPullParser parser, ViewGroup root)
public View inflate (XmlPullParser parser, ViewGroup root, boolean attachToRoot)
public View inflate (int resource, ViewGroup root, boolean attachToRoot)
示意代码:
LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.custom, (ViewGroup)findViewById(R.id.test));
//EditText editText = (EditText)findViewById(R.id.content);// error
EditText editText = (EditText)view.findViewById(R.id.content);
对于上面代码,指定了第二个参数 ViewGroup root,当然你也可以设置为 null 值。
注意:
·inflate 方法与 findViewById 方法不同;
·inflater 是用来找 res/layout 下的 xml 布局文件,并且实例化;
·findViewById() 是找具体 xml 布局文件中的具体 widget 控件(如:Button、TextView 等)。
具体作用:
1、对于一个没有被载入或者想要动态载入的界面,都需要使用LayoutInflater.inflate()来载入;
2、对于一个已经载入的界面,就可以使用Activiyt.findViewById()方法来获得其中的界面元素。
LayoutInflater 是一个抽象类,在文档中如下声明:
public abstract class LayoutInflater extends Object
获得 LayoutInflater 实例的三种方式
1. LayoutInflater inflater = getLayoutInflater();//调用Activity的getLayoutInflater()
2. LayoutInflater inflater = LayoutInflater.from(context);
3. LayoutInflater inflater = (LayoutInflater)context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
其实,这三种方式本质是相同的,从源码中可以看出:
getLayoutInflater():
Activity 的 getLayoutInflater() 方法是调用 PhoneWindow 的getLayoutInflater()方法,看一下该源代码:
public PhoneWindow(Context context)
{
super(context);
mLayoutInflater = LayoutInflater.from(context);
}
可以看出它其实是调用 LayoutInflater.from(context)。
LayoutInflater.from(context):
public static LayoutInflater from(Context context)
{
LayoutInflater LayoutInflater = (LayoutInflater) context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
if (LayoutInflater == null)
{
throw new AssertionError("LayoutInflater not found.");
}
return LayoutInflater;
}
可以看出它其实调用 context.getSystemService()。
结论:所以这三种方式最终本质是都是调用的Context.getSystemService()。
另外getSystemService()是Android很重要的一个API,它是Activity的一个方法,根据传入的NAME来取得对应的Object,然后转换成相应的服务对象。以下介绍系统相应的服务。
传入的Name 返回的对象 说明
WINDOW_SERVICE WindowManager 管理打开的窗口程序
LAYOUT_INFLATER_SERVICE LayoutInflater 取得xml里定义的view
ACTIVITY_SERVICE ActivityManager 管理应用程序的系统状态
POWER_SERVICE PowerManger 电源的服务
ALARM_SERVICE AlarmManager 闹钟的服务
NOTIFICATION_SERVICE NotificationManager 状态栏的服务
KEYGUARD_SERVICE KeyguardManager 键盘锁的服务
LOCATION_SERVICE LocationManager 位置的服务,如GPS
SEARCH_SERVICE SearchManager 搜索的服务
VEBRATOR_SERVICE Vebrator 手机震动的服务
CONNECTIVITY_SERVICE Connectivity 网络连接的服务
WIFI_SERVICE WifiManager Wi-Fi服务
TELEPHONY_SERVICE TeleponyManager 电话服务
inflate 方法
通过 sdk 的 api 文档,可以知道该方法有以下几种过载形式,返回值均是 View 对象,如下:
public View inflate (int resource, ViewGroup root)
public View inflate (XmlPullParser parser, ViewGroup root)
public View inflate (XmlPullParser parser, ViewGroup root, boolean attachToRoot)
public View inflate (int resource, ViewGroup root, boolean attachToRoot)
示意代码:
LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.custom, (ViewGroup)findViewById(R.id.test));
//EditText editText = (EditText)findViewById(R.id.content);// error
EditText editText = (EditText)view.findViewById(R.id.content);
对于上面代码,指定了第二个参数 ViewGroup root,当然你也可以设置为 null 值。
注意:
·inflate 方法与 findViewById 方法不同;
·inflater 是用来找 res/layout 下的 xml 布局文件,并且实例化;
·findViewById() 是找具体 xml 布局文件中的具体 widget 控件(如:Button、TextView 等)。
发表评论
-
ContentProvider之读写短消息
2014-12-08 11:08 561http://blog.csdn.net/liuhe ... -
android之启用默认浏览器
2014-11-03 12:36 474一、启动android默认浏览器 Intent inten ... -
eclipse下看android support v4源码
2014-09-18 22:24 595http://cfy10.blog.51cto.com/707 ... -
Android学习 (七)synchronized
2014-09-17 10:16 577http://hi.baidu.com/fenghuang12 ... -
ScheduledExecutorService定时周期执行指定的任务
2014-09-17 09:57 632http://blog.csdn.net/tsyj810883 ... -
android rotate
2014-09-16 15:21 1168rotate:fromDegrees:其实角度。toDegr ... -
Android获取Manifest中<meta-data>元素的值
2014-09-12 15:39 637在AndroidManifest.xml中,<meta- ... -
PopupWindow
2014-09-12 11:09 8411-初始化 PopupWindow mPop = new P ... -
Android的事件分发onInterceptTouchEvent与onTouchEvent、OnClickListener、OnLongClick
2014-09-11 11:05 864onInterceptTouchEvent()是ViewGro ... -
scrollTo、scrollBy、getScrollX、getScrollY这4个方法的含义,Scroller的简单用法
2014-09-11 10:43 1506scrollTo、scrollBy都是 对 ... -
VelocityTracker
2014-09-11 10:14 644android.view.VelocityTracker主要用 ... -
Android之SurfaceHolder
2014-09-09 16:05 565SurfaceHolder,可以把它当成surface的控制器 ... -
android performClick使用
2014-09-09 13:53 1104performClick 是使用代码主动去调用控件的点击事件( ... -
slidingmenu使用说明
2014-09-07 10:17 581左侧、右侧和两边 在BaseActivity中将Slid ... -
Android之SlidingMenu属性详解
2014-09-07 09:52 619SlidingMenu 常用属性介绍: menu.setMod ... -
Android IOS风格侧边栏效果
2014-08-27 13:45 312http://download.csdn.net/detail ... -
android 代码设置、打开wifi热点及热点的连接
2014-08-26 10:30 999见博客文章 http://blog.csdn.net/luob ... -
Android 之两点触摸技术
2014-08-26 09:58 707package mobile.android.multi.to ... -
Android动画之translate(位移动画)
2014-08-25 15:53 440http://www.cnblogs.com/bavariam ... -
Android 之ViewFlipper实现左右滑动动画效果
2014-08-25 15:31 7421)View切换的控件—ViewF ...
相关推荐
LayoutInflater源码分析 inflate方法的区别
主要介绍了Android LayoutInflater分析的相关资料,需要的朋友可以参考下
Android 中LayoutInflater的使用
LayoutInflater Layout 与 Inflater[扩充]两个单词的合并,可以理解为:当前Activity 布局的扩充.
通过LayoutInflater.Factory实现主题切换功能,详见:http://blog.csdn.net/llew2011
Android 中LayoutInflater(布局加载器)之实战篇 博客的Demo 博客地址: http://blog.csdn.net/l540675759/article/details/78112989 两种方式实现小红书的引导页: (1)自定义View (2)自定义LayoutInflater....
NULL 博文链接:https://lxmgfd.iteye.com/blog/979716
LayoutInflater.from(context).inflate()方法的调研
android inflate demo,方便新手使用,下载导入android studio就可直接运行,简单明了的展现inflate不同参数的区别
在android中,LayoutInflater有点类似于Activity的findViewById(id),不同的是LayoutInflater是用来找layout下的xml布局文件,并且实例化!而findViewById()是找具体xml下的具体 widget控件(如:Button,TextView等)。...
NULL 博文链接:https://yufeng-zhu.iteye.com/blog/1676207
android中LayoutInflater的使用.pdf
JavaScript应用实例-LayoutInflater.js
Android LayoutInflater加载布局详解 对于有一定Android开发经验的同学来说,一定使用过LayoutInflater.inflater()来加载布局文件,但并不一定去深究过它的原理,比如 1.LayoutInflater为什么可以加载layout文件? ...
down-test-Android 获得 LayoutInflater 实例的三种方式
在平时的开发过程中,我们经常会用LayoutInflater这个类,比如说在Fragment$onCreateView和RecyclerView.Adapter$onCreateViewHolder中都会用到。它的用法也无非就是LayoutInflater.inflate(resourceId, root, ...
这是老外写的经典Android代码,对学习AsyncTask、JSONAdapter、LayoutInflater很有用