LayoutInflater.from(this)的用法
2012-07-30 16:37
387 查看
通俗的说,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)肯定会报错.
因为在一个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.from(this)的用法
- LayoutInflater.from(this)的用法--inflate就相当于将一个xml中定义的布局找出来.
- LayoutInflater.from(this)的用法
- 自定义组合控件关于LayoutInflater.from(context).inflate(R.layout.view_title, this,true)的问题
- LayoutInflater.from(this)
- LayoutInflater.from(this).inflate()参数解析
- LayoutInflater.from(this) 这个this是什么,加载完后的View 就是谁的。
- LayoutInflater.from(this) View.inflate()
- LayoutInflater.from(this) View.inflate()
- LayoutInflater.from(this)、inflate 详解
- LayoutInflater的inflate函数用法详解
- LayoutParams的用法,及LayoutInflater区别
- 安卓笔记:LayoutInflater.inflate的用法
- LayoutInflater的inflate函数用法详解
- LayoutInflater.inflate()用法
- 对于LayoutInflater.from(parent.getContext()).inflate()方法的笔记
- This application is modifying the autolayout engine from a background thread after the engine was ac
- LayoutInflater的inflate函数用法详解
- View.Inflate)与LayoutInflater.from().inflate();
- LayoutInflater和MenuInflater用法