这篇文章给大家介绍增压泵如何在Android中使用,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
1,对于一个没有被载入或者想要动态载入的界面,都需要使用LayoutInflater.inflate()来载入;
2,对于一个已经载入的界面,就可以使用Activiyt.findViewById()方法来获得其中的界面元素。
文档中的声明:
public abstract class LayoutInflater extends 对象
<强>三种实例化方式:强>
1。LayoutInflater增压泵=getLayoutInflater ();//调用活动的getLayoutInflater ()
2。LayoutInflater localinflater=(LayoutInflater) context.getSystemService (Context.LAYOUT_INFLATER_SERVICE);
3。LayoutInflater增压泵=LayoutInflater.from(上下文);
其实,这三种方式本质是相同的,从源码中可以看出:
getLayoutInflater ():
活动的getLayoutInflater()方法是调用PhoneWindow的getLayoutInflater()方法,看一下该源代码:
, public PhoneWindow (Context 上下文),{, ,,,超级(上下文),, ,,,mLayoutInflater =, LayoutInflater.from(上下文),, }
可以看出它其实是调用LayoutInflater.from(上下文)。
LayoutInflater.from(上下文):
public static LayoutInflater 从(Context 上下文),{,, LayoutInflater 才能;LayoutInflater =,, ,,,,,(LayoutInflater), context.getSystemService (Context.LAYOUT_INFLATER_SERVICE);,, if 才能;(LayoutInflater ==, null), {,, ,,,throw new AssertionError (“LayoutInflater  not 发现!”);,, ,,}, return 才能;LayoutInflater;,, }
可以看出它其实调用context.getSystemService ()。
结论:所以这三种方式最终本质是都是调用的context.getSystemService ()。
<>强膨胀方法强>
通过sdk的api文档,可以知道该方法有以下几种过载形式,返回值均是视图对象,如下
, public View inflate (int 资源,ViewGroup 根) public View  inflate (XmlPullParser 解析器,,ViewGroup 根) public View  inflate (XmlPullParser 解析器,,ViewGroup 根,,boolean attachToRoot) public View  inflate (int 资源,,ViewGroup 根,,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 根,当然你也可以设置为,null 值。* *
<强>注意:强>
·膨胀方法与findViewById方法不同,
·增压泵是用来找res/布局下的xml布局文件,并且实例化;
·findViewById()是找具体xml布局文件中的具体部件控件(如:按钮,TextView等)。
关于增压泵如何在Android中使用就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看的到。