- 浏览: 334738 次
- 性别:
- 来自: 上海
-
文章分类
最新评论
-
xuanyuanxiaoxue:
...
Android - LayoutInflater -
柴兴博:
不错 多谢
Android 悬浮Activity并可拖动(访悬浮歌词) -
di1984HIT:
写的很好,我收藏一下。
java之动态代理模式(JDK和cglib) -
chinacssnj:
待测试,明天测,测试的结果发给大家
网络开发上传文件到服务器 -
fx_199182:
...
Android之MediaPlayer
在实际开发中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 localinflater = (LayoutInflater)context.getSystemService (Context.LAYOUT_INFLATER_SERVICE); 3. LayoutInflater inflater = LayoutInflater.from(context);
其实,这三种方式本质是相同的,从源码中可以看出:
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()。
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)
1:
public View inflate (int resource, ViewGroup root)
reSource:View的layout的ID
root:如果为null,则将此View作为根,此时既可以应用此View中的其他控件了。
如果!null, 则将默认的layout作为View的根。
2:
public View inflate ( XmlPullParser parser, ViewGroup root)
parser:你需要解析xml的解析接口
root:如果null,则将此View作为根,此时既可以应用此View中的其他控件了。
如果!null, 则将默认的layout作为View的根。
3:
public View inflate ( XmlPullParser parser, ViewGroup root, boolean attachToRoot)
parser:你需要解析View的xml的解析接口
root:如果null,则将此View作为根,此时既可以应用此View中的其他控件了。
如果!null, 则将默认的layout作为View的根。
attachToRoot:
ture:也就将此解析的xml作为View根
fase:则为默认的xml,做为根视图View
4:
public View inflate (int resource, ViewGroup root, boolean attachToRoot)
resource:View的layout的ID
root:如果null,则将此View作为根,此时既可以应用此View中的其他控件了。
如果!null, 则将默认的layout作为View的根。
attachToRoot:
ture:也就将此解析的xml作为View根
fase:则为默认的xml,做为根视图View
示意代码:
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);
同时在此讲讲让我去API中去理解这四个函数的原因吧!嘿嘿!你肯定又会多学一招!
在Activity中:
大家是否知道,在setContentView(new MySurfaceView(this))后,此Activity中声明的View控件,
如:TextView 为什么引用不到layout布局文件中的控件ID呢!初一看能够应用到,但是为什么编译就报空指针呢!原因:在setContentView(new MySurfaceView(this))后,此时的View变为了根视图了,虽然能应用到TextView对应的ID,但是我在 MySurfaceView中根本就没有这个对象,所以就报空指针咯!解决办法:
View view = LayoutInflater.from(this).inflate(R.layout.passover, null);注:每解析一次都会产生不同的对象
然后你再引用没问题,使用自如了。
发表评论
-
Android中AsyncTask的简单用法
2012-01-13 16:00 1189在开发Android移动客户端的时候往往要使用多线程来进行操 ... -
Android应用的自动升级、更新模块的实现 .
2011-11-16 14:01 692http://www.eoeandroid.com/threa ... -
一个APK反编译利器Apktool
2011-11-16 13:54 1613一个APK反编译利器Apktool APK 本地化 ... -
自定义Android标题栏TitleBar布局
2011-11-14 14:13 1278很多网友发现自己Android程序的标题栏TitleBar区域 ... -
Android GPS获取地理位置 .
2011-11-14 14:11 880import android.app.Activity; i ... -
android ListView详解
2011-11-14 13:48 1077在android开发中ListView是比较常用的组件,它以列 ... -
Android之Content provider 详解
2011-11-14 13:35 2503Android是如何实现应用程序之间数据共享的?一个应用程序可 ... -
Android源码地址
2011-11-12 19:14 1070http://blog.csdn.net/ilittleone ... -
android之File
2011-11-11 22:39 20301:Fileservice package cn.itcas ... -
Android知识补漏
2011-11-09 22:33 01:AndroidManifiest.xml < ... -
深入剖析Android消息机制
2011-11-09 14:13 978在Android中,线程内部或者线程之间进行信息交互时经常会使 ... -
Android之Handler详解(四)
2011-11-09 14:00 1301d、自己创建新的线程,然后在新线程中创建Looper,主线程调 ... -
Android之Handler详解(三)
2011-11-09 13:58 1380c、将消息队列绑定到子线程上,主线程只管通过Handl ... -
Android之Handler详解(二)
2011-11-09 13:54 1691二:sendMessage版本的Handl ... -
Android之Handler详解(一)
2011-11-09 13:22 2324一个Handler允许你发送和处理消息(Message)以及 ... -
关于StartActivityForResult方法的使用
2011-10-31 17:11 1120根据方法名可知 这个方法是要得到启动后的Activity返回的 ... -
Android 悬浮Activity并可拖动(访悬浮歌词)
2011-10-24 16:23 2131天天动听, 这款Android手机上的音乐播放器,相信不少朋友 ... -
Android GWES
2011-10-24 16:13 1224第八章 Android GWES 8.1 View Syst ... -
Android系统服务-WindowManager
2011-10-24 16:10 1474WindowManager是Android中一个重要的服务 ... -
http通信
2011-10-15 17:31 1115HTTP(HyperText Transfer Proto ...
相关推荐
down-test-Android 获得 LayoutInflater 实例的三种方式
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(getContainerView(), container, false); aq = new AQuery(getActivity(),...
在Android开发中,`LayoutInflater`是一个非常重要的工具类,它主要用于将XML布局文件转换为视图对象,使得我们可以动态地将界面元素添加到应用程序中。`LayoutInflater`是Android框架的一部分,它极大地增强了UI...
在Android开发中,LayoutInflater是一个非常关键的工具,它主要用于将XML布局文件转换为视图对象。这个过程称为“实例化”或“.inflate”。通过LayoutInflater,我们可以动态地在运行时加载和插入用户界面元素,这...
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout, parent, false); return new MyViewHolder(itemView); } @Override public void onBindViewHolder(MyViewHolder ...
同时,对于动态布局,可能涉及到LayoutInflater和DataBinding。 6. **权限管理** 在Android 6.0及以上版本,需要处理运行时权限。源码可能会包含如何使用` ActivityCompat`和`PermissionCompat`进行权限请求和管理...
Android 中LayoutInflater(布局加载器)之实战篇 博客的Demo 博客地址: http://blog.csdn.net/l540675759/article/details/78112989 两种方式实现小红书的引导页: (1)自定义View (2)自定义LayoutInflater....
在Android开发中,`LayoutInflater`是一个至关重要的工具类,它主要负责将XML布局文件转换成View对象并添加到视图层次结构中。`LayoutInflater`的名字来源于"Layout Inflater",正如描述中提到的,它是对当前...
- 使用LayoutInflater加载布局,并将其设置为PopupWindow的内容视图。 - 设置PopupWindow的宽度和高度,一般宽度设为MATCH_PARENT,高度可以是WRAP_CONTENT或根据需要设定的具体值。 - 设置PopupWindow是否能获得...
在Android应用开发中,换肤功能是一个非常受欢迎的特性,它允许用户根据个人喜好更改应用程序的界面风格。"android-skin-loader"是一个专门为Android平台设计的插件化换肤库,方便开发者集成到自己的项目中,为用户...
在Android开发中,`LayoutInflater` 是一个至关重要的工具,它负责将XML布局文件转换为视图对象(View objects)。这个过程被称为布局的“实例化”或“膨胀”。`LayoutInflater` 提供了一种灵活的方式来动态地加载和...
通过`LayoutInflater`将布局加载到Dialog中。 3. **DialogFragment**:为了保持良好的架构和避免内存泄漏,Android推荐使用`DialogFragment`来管理Dialog。`DialogFragment`继承自`Fragment`,可以独立于Activity...
arser.START_TAG && type != XmlPullParser.END_DOCUMENT) { // Do nothing } if (type != XmlPullParser.START_TAG) { throw new InflateException(parser.getPositionDescription() + ": No start tag found!...
Android LayoutInflater 是一个核心组件,它负责将XML布局文件转换为屏幕上的可交互视图对象。在Android应用开发中,我们通常使用LayoutInflater来动态地加载和插入布局,这在创建自定义视图、处理动态数据或者在...
测试:Android 中LayoutInflater的使用 注意:Aj_04是用了调用另外一个界面,要注意调用的方法, 还一定还要在AndroidManifest.xml 中加上呢句:<activity android:name="LayoutInflaterDemo"></activity>
11. **建造者模式**:用于构建复杂对象,例如,使用Android的LayoutInflater构建View层次结构。 通过"android-patterns-master.zip"中的示例,你可以深入理解这些设计模式在Android开发中的实际应用,并能将它们...
`LayoutInflater`用于动态加载布局,提供更灵活的界面设计。此外,`LiveData`和`ViewModel`(来自Android Architecture Components)可能用于实现数据的实时更新,使得界面能在数据变化时自动更新,提高用户体验。 ...
在Android开发中,`SwipeListView`是一个非常实用的视图组件,它提供了滑动操作来显示额外的内容或者执行相应的动作,比如在列表项上滑动显示删除按钮。本示例将详细讲解如何在项目中使用`android-swipelistview`库...
首先,我们需要理解`LayoutInflater`在Android中的角色。`LayoutInflater`是负责将XML布局文件转换为视图对象(View)的关键类。它从资源文件中读取布局描述,并根据描述创建对应的View实例。通过`LayoutInflater`,...