android和view相关的东西
findViewById(R.id.xxxx),把xml里面的控件根据id绑定到类成员变量,适用于普通控件和布局
LayoutInflater,用于把布局xml里面的东西实例化(会重新实例化,和findViewById不同),sample代码如下
LayoutInflater inflator = getLayoutInflater();
//注意,这里参数是layout,不是id
RelativeLayout layout = (RelativeLayout)inflator.inflate(R.layout.activity_main, null);
TextView aTextView = new TextView(this);
aTextView.setWidth(100);
aTextView.setHeight(100);
aTextView.setText("asdasdasdasd");
layout.addView(aTextView);
//采用LayoutInflater的需要重新设置布局才有效果。
setContentView(layout);
附三种使用LayoutInflater的方法
LayoutInflater inflater = LayoutInflater.from(this);
View layout = inflater.inflate(R.layout.main, null);
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.main, null);
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.main, null);
更多相关文章
- 一个侧屏滑动操作的实例(仿遇见)之三:代码分析
- Android(安卓)Design Support Library之TabLayout
- Android的冷启动优化
- Android(安卓)ListView Adapter的getItemViewType和getViewTypeC
- Android快速开发框架dyh详解(二)---控件层的使用
- 第一行代码Android笔记精华版
- android 基础控件(EditView、SeekBar等)的属性及使用方法
- Android使用newInstance()实例化fragment&setArguments()传递参
- Android中RelativeLayout的使用-----------转载下----以免忘记.