inflate与findViewById

来源:互联网 发布:php关闭微信浏览器 编辑:程序博客网 时间:2024/05/18 00:34

总结的来说,inflate方法获得布局,findViewById方法获得布局下的控件




在活动类中,直接使用findViewById(),是获得指定布局(setContentView()设置的布局)下的控件



在非活动类中使用inflate方法获得布局

先获取LayoutInflater对象:

LayoutInflater mInflater=LayoutInflater.from(this);//LayoutInflater.from(getContext());


bView=mInflater.inflate(R.layout.XX_item,null);


或者直接

bView=LayoutInflater.from(this).inflate(R.layout.XX_item,null);

得到布局,我们就可以用bView.findViewById()得到相应布局文件里的某些控件。

例如:

TextView textView=(TextView) bView.findViewById(R.id.text_View);

textView.setText......



0 0
原创粉丝点击