Android(安卓)Inflate方法
16lz
2021-01-26
Android Inflate方法
通俗的说,inflate就相当于将一个xml中定义的布局找出来.在一个Activity里如果直接用findViewById()的话,相应的是setConentView()所对应layout里的组件.如果不是,你就必须用inflate()先将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(intresource,ViewGrouproot,booleanattachToRoot)
更多相关文章
- Android(安卓)自定义RecyclerView 实现真正的Gallery效果
- Android(安卓)listview 实现列表多选
- android 控制震动强度
- Android使用广播(BroadCast)实现强制下线的方法
- Google Map API Key 获得方法
- Android开发8:UI组件TextView,EditText,Button
- android 植入点乐广告方法
- UI与线程交互
- Android_Layout_xml布局