Android(安卓)inflate的三种使用方法
16lz
2021-01-26
Android inflate的三种用法
inflate是用来把XML定义好的布局找出来,inflate之后并没有直接显示,需要再加入到其他布局当中才能显示,以下是inflate的三种使用方法.
- 使用LayoutInflater.inflater方法
- 使用context.getSystemService方法
- 使用View.inflate方法
//方法3:使用View.inflate方法 private void inflate3() { LinearLayout sub = (LinearLayout) View.inflate(this, R.layout.my_layout, null); LinearLayout layout = (LinearLayout) findViewById(R.id.activity_main); layout.addView(sub); } //方法2:使用context.getSystemService方法 private void inflate2() { LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); LinearLayout sub = (LinearLayout) inflater.inflate(R.layout.my_layout, null); LinearLayout layout = (LinearLayout) findViewById(R.id.activity_main); layout.addView(sub); } //方法1:使用LayoutInflater.inflater方法 private void inflate1() { LayoutInflater inflater = LayoutInflater.from(this); LinearLayout sub = (LinearLayout) inflater.inflate(R.layout.my_layout, null); LinearLayout layout = (LinearLayout) findViewById(R.id.activity_main); layout.addView(sub); }
代码地址
更多相关文章
- Android(安卓)自定义LayoutManager
- Android布局管理(4)
- 【Android(安卓)界面效果43】Android(安卓)LayoutInflater的infl
- Android开发之MediaPlayer使用
- android 之 Inflate
- Android从零开始(十五)
- Android(安卓)strings.xml中空格符的标识方法附xml特殊字符表示
- Android(安卓)编码规范 | 代码风格指南
- Android(安卓)Appcompat样式使用自定义Dialog