android中LayoutInflater详解与使用
2013-07-16 21:46
423 查看
android的LayoutInflater用来得到一个布局文件,也就是xxx.xml,而我们常用的findviewbyid是用来取得布局文件里的控件或都布局。inflater即为填充的意思,也就是说得到一个布局文件来填充(把布局文件实例化成一个View然后返回该view)。使得方法如下: 三种办法可以得到:
1:在activity中的话可以直接调用getLayoutInflater()来获得。
2:通过服务获得:getSystemService. (Context.LAYOUT_INFLATER_SERVICE)。
3:通过ayoutInflater.from(Context context)方法来获得。 三种方法的代码如下:
在activity中的话上面的context可直接换成this。 得到返回的View后我们可以用他来给布局文件里的控件进行相应设置,如:
上面的获得View方法有两个参数,lf.inflate(int resource, ViewGroup root),第一个为整型的资源ID,也就是xml布局文件的id,后面为根View,如果为null则只创建和返回View,如果传入root,则把创建的View加为根View的子view。 LayoutInflater可以用来得到布局文件对里面的控件进行设置,也可以可以将得到的View返回给fragment,adapter等。 总之想怎么用就怎么用了...
1:在activity中的话可以直接调用getLayoutInflater()来获得。
2:通过服务获得:getSystemService. (Context.LAYOUT_INFLATER_SERVICE)。
3:通过ayoutInflater.from(Context context)方法来获得。 三种方法的代码如下:
//方法3: LayoutInflater lf=LayoutInflater.from(context); View tempView=lf.inflate(R.layout.select_list, null); //方法2: LayoutInflater lf = (LayoutInflater)context.getSystemService.(Context.LAYOUT_INFLATER_SERVICE); View tempView=lf.inflate(R.layout.select_list, null); //方法1: LayoutInflater lf = LayoutInflater.from(context); View tempView=lf.inflate(R.layout.select_list, null);
在activity中的话上面的context可直接换成this。 得到返回的View后我们可以用他来给布局文件里的控件进行相应设置,如:
TextView id=(TextView) tempView.findViewById(R.id.id); TextView address=(TextView) tempView.findViewById(R.id.address); id.setText("123456"); address.setText("中国湖南长沙");
上面的获得View方法有两个参数,lf.inflate(int resource, ViewGroup root),第一个为整型的资源ID,也就是xml布局文件的id,后面为根View,如果为null则只创建和返回View,如果传入root,则把创建的View加为根View的子view。 LayoutInflater可以用来得到布局文件对里面的控件进行设置,也可以可以将得到的View返回给fragment,adapter等。 总之想怎么用就怎么用了...
相关文章推荐
- Android LayoutInflater 详解, 初次使用inflater 这位仁兄说明的很清楚了。。。
- android动态设置布局LayoutInflater的使用详解
- Android:LayoutInflater的使用
- <转> Android LayoutInflater详解
- Android 中LayoutInflater的使用
- Android编程之LayoutInflater的inflate方法详解
- 【android】LayoutInflater.inflate方法的详解及xml根元素的布局参数不起作用的问题 - u011494050的专栏 - 博客频道 - CSDN.NET
- Android_LayoutInflater的作用与用法详解
- Android LayoutInflater详解
- Android中LayoutInflater的使用
- Android中的android:layout_weight使用详解
- Android 中LayoutInflater的使用
- 【android】LayoutInflater.inflate方法的详解及xml根元素的布局参数不起作用的问题
- android-----LayoutInflater的inflate方法详解
- Android LayoutInflater.inflate的使用及源码分析
- Android LayoutInflater详解
- Android LayoutInflater的理解和使用
- android 中LayoutInflater 的使用
- Android高手进阶教程(五)之----Android 中LayoutInflater的使用!
- Android LayoutInflater详解