关于inflate,获取布局xml
2011-02-14 23:13
323 查看
通俗的说,inflate就相当于将一个xml中定义的布局找出来.
因为在一个Activity里如果直接用findViewById()的话,对应的是setConentView()的那个layout里的组件.
因此如果你的Activity里如果用到别的layout,比如对话框上的layout,你还要设置对话框上的layout里的组件(像图片 ImageView,文字TextView)上的内容,你就必须用inflate()先将对话框上的layout找出来,然后再用这个layout对象去找到它上面的组件,如:
View view = View.inflate(this, R.layout.dialog_layout, null);
TextView dialogTV = (TextView) view.findViewById(R.id.dialog_tv);
dialogTV.setText("abcd");
如果组件R.id.dialog_tv是对话框上的组件,而你直接用this.findViewById(R.id.dialog_tv)肯定会报错.
三种方式可以生成LayoutInflater :
LayoutInflater inflater = LayoutInflater.from(this);
LayoutInflater inflater = getLayoutInflater();
LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
然后调用inflate方法将xml布局文件转成View
public View inflate (int resource, ViewGroup root, boolean attachToRoot)
在View类中,也有inflate方法
public static View inflate (Context context, int resource, ViewGroup root)
转自:http://daydayup1989.javaeye.com/blog/824735
因为在一个Activity里如果直接用findViewById()的话,对应的是setConentView()的那个layout里的组件.
因此如果你的Activity里如果用到别的layout,比如对话框上的layout,你还要设置对话框上的layout里的组件(像图片 ImageView,文字TextView)上的内容,你就必须用inflate()先将对话框上的layout找出来,然后再用这个layout对象去找到它上面的组件,如:
View view = View.inflate(this, R.layout.dialog_layout, null);
TextView dialogTV = (TextView) view.findViewById(R.id.dialog_tv);
dialogTV.setText("abcd");
如果组件R.id.dialog_tv是对话框上的组件,而你直接用this.findViewById(R.id.dialog_tv)肯定会报错.
三种方式可以生成LayoutInflater :
LayoutInflater inflater = LayoutInflater.from(this);
LayoutInflater inflater = getLayoutInflater();
LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
然后调用inflate方法将xml布局文件转成View
public View inflate (int resource, ViewGroup root, boolean attachToRoot)
在View类中,也有inflate方法
public static View inflate (Context context, int resource, ViewGroup root)
转自:http://daydayup1989.javaeye.com/blog/824735
相关文章推荐
- 关于inflate,获取布局xml
- ViewPager中动态添加XML布局文件,并获取XML文件中控件ID的操作(inflate)
- 使用inflate获取XML布局文件
- 关于masonry布局中的frame和bounds 获取
- 关于安卓XML布局里面的引用与代码顺序
- LayoutInflater和inflate()方法的用法 (自定义View中加载xml布局)
- 关于使用eclipse开发安卓时布局界面XML的graphical layout没有显示的问题
- 1. 一个界面xml布局文件应仅inflate一次即可
- 关于自定义布局,xml中配置属性(attrs)
- 关于android xml布局文件中android:tools 标签的认识
- 关于 Fragment 之3___fragment在xml布局中写法
- 关于 Fragment 之3___fragment在xml布局中写法
- 关于Microsoft.XMLHTTP或Ajax中异步获取数据的问题
- Android XML布局中关于资源的使用
- 关于Android中xml布局文件
- 替换Manifest.xml,界面替换(动态布局、动态获取R文件)
- android关于获取布局宽高的问题
- 关于解决无法获取AJAX中无法获取XML返回的问题
- 关于获取布局的宽高
- 关于横竖屏切换获取和设置布局的补充