LayoutInflater作用及使用
作用:
1、对于一个没有被载入或者想要动态载入的界面, 都需要使用inflate来载入.
2、对于一个已经载入的Activity, 就可以使用实现了这个Activiyt的的findViewById方法来获得其中的界面元素.
方法:
Android里面想要创建一个画面的时候, 初学一般都是新建一个类, 继承Activity基类, 然后在onCreate里面使用setContentView方法来载入一个在xml里定义好的界面.
其实在Activity里面就使用了LayoutInflater来载入界面, 通过getSystemService(Context.LAYOUT_INFLATER_SERVICE)方法可以获得一个 LayoutInflater, 也可以通过LayoutInflater inflater = getLayoutInflater();来获得.然后使用inflate方法来载入layout的xml,

package com.example.inflateapp; import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView; public class MainActivity extends Activity implements OnClickListener{ private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); button=(Button) this.findViewById(R.id.button1);
button.setOnClickListener(this);
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
} @Override
public void onClick(View v) {
// TODO Auto-generated method stub
showMyDialog();
}
void showMyDialog(){
AlertDialog.Builder buider;
AlertDialog alertDialog;
Context mcontext=MainActivity.this; LayoutInflater inflater=(LayoutInflater) mcontext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout=inflater.inflate(R.layout.custom_dialog, null);
TextView text=(TextView) layout.findViewById(R.id.textView1);
text.setText("LayoutInflater作用及使用 code by 子夜双鱼");
buider=new AlertDialog.Builder(mcontext);
buider.setView(layout);
alertDialog=buider.create();
alertDialog.show(); }
}
最近做项目时研究一个开源 东西,对Inflate不太理解,查有道“使充气”,我的理解就是把视图布局显示出来的意思。于是有了上文。
LayoutInflater作用及使用的更多相关文章
- 【Android 界面效果23】LayoutInflater作用及使用
作用: 1.对于一个没有被载入或者想要动态载入的界面, 都需要使用inflate来载入. 2.对于一个已经载入的Activity, 就可以使用实现了这个Activiyt的的findViewById方 ...
- 转:LayoutInflater作用及使用
作用: 1.对于一个没有被载入或者想要动态载入的界面, 都需要使用inflate来载入. 2.对于一个已经载入的Activity, 就可以使用实现了这个Activity的的findViewById() ...
- LayoutInflater作用及使用--自定义EditText,自带清除内容按钮
作用: 1.对于一个没有被载入或者想要动态载入的界面, 都需要使用inflate来载入. 2.对于一个已经载入的Activity, 就可以使用实现了这个Activiyt的的findViewById方法 ...
- LayoutInflater作用及使用(转)
作用: 1.对于一个没有被载入或者想要动态载入的界面, 都需要使用inflate来载入. 2.对于一个已经载入的Activity, 就可以使用实现了这个Activiyt的的findViewById方法 ...
- LayoutInflater作用是将layout的xml布局文件实例化为View类对象。
获取LayoutInflater的方法有如下三种: LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.L ...
- 转载《 LayoutInflater 的inflate函数用法详解》
很多人在网上问LayoutInflater类的用法,以及inflate()方法参数的含义,现解释如下: inflate()的作用就是将一个用xml定义的布局文件查找出来,注意与findViewById ...
- Android—LayoutInflater学习笔记
LayoutInflater的的主要用处:使用LayoutInflater来载入界面,类似于findViewById()在Activity中加载Widget组件一样.区别在于与LayoutInflat ...
- LayoutInflater的inflate函数用法详解
LayoutInflater作用是将layout的xml布局文件实例化为View类对象. 获取LayoutInflater的方法有如下三种: LayoutInflater inflater=(Layo ...
- LayoutInflater和inflate()方法的用法
LayoutInflater作用是将layout的xml布局文件实例化为View类对象. 实现LayoutInflater的实例化共有3种方法, (1).通过SystemService获得 Layou ...
随机推荐
- 普林斯顿大学算法课 Algorithm Part I Week 3 比较器 Comparators
比较器接口(Comparator interface):用可选顺序(alternate order)进行排序 public interface Comparator<key> int co ...
- AndroidUI 视图动画-透明动画效果 (AlphaAnimation)
1.新建一个Android项目,Activity添加一个按钮如下代码: <Button android:id="@+id/btnAiphaAnimation" android ...
- javascript数组去重算法-----2
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 通信基站(dfs回溯,思维)
Description Input Output
- MySQL 查询结果以百分比显示
找了一些资料,然后我是用到了MySQL字符串处理中的两个函数concat()和left()1.[CONCAT(str1,str2,...) 返回来自于参数连结的字符串.如果任何参数是 NULL, 返 ...
- MAC COCOA call command 调用终端控制台程序
MAC COCOA call command 调用终端控制台程序 STEP 1 先写一个C++ DOS程序 STEP2 使用NSTask来运行,然后用NSPipe和 NSData来接受运行的结果字符串 ...
- Sass介绍及入门教程
Sass是什么? Sass是"Syntactically Awesome StyleSheets"的简称.那么他是什么?其实没有必要太过于纠结,只要知道他是“CSS预处理器”中的一 ...
- JavaScript 继承方式的实现
1.原型链继承 function superType(name){ this.name= 'milk'; } super.prototype.sayName=function(){ console.l ...
- 浅谈C中的指针和数组(五)
前面写了一些C指针和数组的一些知识,但是还有一些很重要的知识没有交代,这里做一个补充. 首先看一下,普通变量(指针也是变量)和数组名查看地址的方式是不同的. 查看数组变量的地址,不需要使用 & ...
- Apache配置参数
Apache的配置文件 配置文件所在目录:/etc/httpd/conf/主配置文件:httpd.conf旧版本中的配置文件:资源配置文件:srm.conf访问许可权配置文件:access.conf ...