首先对LayoutInflater下一个定义吧,Layoutinflater的作用就是将一个xml布局文件实例化为View对象。
获取Layoutinflater对象的方法有三种,招不在多,管用就行,跟踪源码后发现三种方法的本质都是调用了context.getSystemService(),所以建议以后写的时候就用context.getSystemService()。具体写法如下:
LayoutInflater inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
接下来重点介绍
inflate()方法的使用:
1、返回值:
View对象,有了View对象我们就能做诸如setContentView()或者在自定义Adapter里面的getView()方法中获取item的布局。
2、参数:
public View inflate (int resource, ViewGroup root)
第一个参数是一个xml文件的R索引,比如R.layout.activity_main。第二个参数如果不为null就是将得到的View对象再放入一个Viewgroup容器中,我这么说可能有的读者还不是很清楚,文章的最后会有一个例子来说明问题。
public View inflate (int resource, ViewGroup root, boolean attachToRoot)
这里的第三个参数的意思是如果第二个参数不为null,那么为true则放入第二个参数引用的Viewgroup中,为false则不放入第二个参数的Viewgroup中。
下面用一个实例来说明问题。
布局文件有两个:
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<RelativeLayout
android:id="@+id/viewgroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</RelativeLayout>
</LinearLayout>
inflatertest.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="inflatedemo" />
</LinearLayout>
当
Activity中如是写:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewGroup = (RelativeLayout) findViewById(R.id.viewgroup);
mInflater = (LayoutInflater) getApplicationContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
mInflater.inflate(R.layout.inflatertest, mViewGroup);
}
或者
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewGroup = (RelativeLayout) findViewById(R.id.viewgroup);
mInflater = (LayoutInflater) getApplicationContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
mInflater.inflate(R.layout.inflatertest, mViewGroup, true);
}
结果是这样子的:
当
Activity中如是写:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewGroup = (RelativeLayout) findViewById(R.id.viewgroup);
mInflater = (LayoutInflater) getApplicationContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
mInflater.inflate(R.layout.inflatertest, mViewGroup, false);
}
结果是这样的:
今天就到这里,学习框架,一定要掌握函数的返回值和参数的意义,学会总结规律。
分享到:
相关推荐
Android 中LayoutInflater的使用
android中LayoutInflater的使用.pdf
测试:Android 中LayoutInflater的使用 注意:Aj_04是用了调用另外一个界面,要注意调用的方法, 还一定还要在AndroidManifest.xml 中加上呢句:<activity android:name="LayoutInflaterDemo"></activity>
Android 中LayoutInflater(布局加载器)之实战篇 博客的Demo 博客地址: http://blog.csdn.net/l540675759/article/details/78112989 两种方式实现小红书的引导页: (1)自定义View (2)自定义LayoutInflater....
主要介绍了Android LayoutInflater分析的相关资料,需要的朋友可以参考下
LayoutInflater Layout 与 Inflater[扩充]两个单词的合并,可以理解为:当前Activity 布局的扩充.
NULL 博文链接:https://yufeng-zhu.iteye.com/blog/1676207
down-test-Android 获得 LayoutInflater 实例的三种方式
主要介绍了Android 中LayoutInflater.inflate()方法的介绍的相关资料,希望通过本文大家能掌握这部分内容,需要的朋友可以参考下
LayoutInflater类在我们日常开发中经常会用到,最近在使用中就遇到了一些问题,所有下面这篇文章主要给大家总结了关于Android中使用LayoutInflater要注意的一些坑,希望通过这篇能让大家避免走一些弯路,需要的朋友...
在android中,LayoutInflater有点类似于Activity的findViewById(id),不同的是LayoutInflater是用来找layout下的xml布局文件,并且实例化!而findViewById()是找具体xml下的具体 widget控件(如:Button,TextView等)。...
android inflate demo,方便新手使用,下载导入android studio就可直接运行,简单明了的展现inflate不同参数的区别
主要介绍了Android开发实现自定义Toast、LayoutInflater使用其他布局,涉及Android自定义Toast与界面布局相关操作技巧,需要的朋友可以参考下
通过LayoutInflater.Factory实现主题切换功能,详见:http://blog.csdn.net/llew2011
关于Android的LayoutInflater的使用!
LayoutInflater在开发中使用频率很高,但是一直没有太知道LayoutInflater.from(context).inflate()的真正用法,今天就看看源码的流程。 首先来看from()的源码: /** * Obtains the LayoutInflater from the ...
3.我们定义View的时候,如果需要在布局中使用,则必须实现带AttributeSet参数的构造方法,这又是为什么呢? 既然在这篇文章提出来,那说明这三个问题都是跟LayoutInflater脱不了干系的。在我们的分析过程中,会对...
主要介绍了Android开发之获取LayoutInflater对象的方法,结合实例形式总结分析了Android获取LayoutInflater对象的常用技巧,需要的朋友可以参考下