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

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView; public class MainActivity extends Activity implements OnClickListener { private Button button; public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); button = (Button) findViewById(R.id.button);
button.setOnClickListener(this);
} @Override
public void onClick(View v) { showCustomDialog();
} public void showCustomDialog() {
AlertDialog.Builder builder;
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.text);
text.setText("Hello, Welcome to Mr Wei's blog!");
ImageView image = (ImageView) layout.findViewById(R.id.image);
image.setImageResource(R.drawable.icon);
builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
alertDialog = builder.create();
alertDialog.show();
}
}
转:LayoutInflater作用及使用的更多相关文章
- LayoutInflater作用及使用
		
作用: 1.对于一个没有被载入或者想要动态载入的界面, 都需要使用inflate来载入. 2.对于一个已经载入的Activity, 就可以使用实现了这个Activiyt的的findViewById方法 ...
 - 【Android 界面效果23】LayoutInflater作用及使用
		
作用: 1.对于一个没有被载入或者想要动态载入的界面, 都需要使用inflate来载入. 2.对于一个已经载入的Activity, 就可以使用实现了这个Activiyt的的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 ...
 
随机推荐
- Clone table header and set as the first element, and replace header's th with td
			
Clone table header and replace header's th with td var tableHeaderRow = '#tableId tbody tr:nth-child ...
 - Pandas简易入门(二)
			
目录: 处理缺失数据 制作透视图 删除含空数据的行和列 多行索引 使用apply函数 本节主要介绍如何处理缺失的数据,可以参考原文:https://www. ...
 - 实现在DevExpress.XtraGrid.GridControl的列头绘制复选框以实现全选的功能
			
首先新建一个Win Form测试项目,拖一个GridControl控件到窗体上. public partial class Form1 : Form { public Form1() { Initia ...
 - c#中sqlhelper类的编写(一)
			
在.net平台的项目开发中,凡是用到数据库交互的,都有必要了解SqlHelper类的原理. 步骤一: 我就拿WPF项目开发作为例子.首先要新建一个App.config(应用程序配置文件).注意,在VS ...
 - 扩展ServiceHost<T>类
			
public class ServiceHost<T> : ServiceHost { public void EnableMetadataExchange(bool enableHttp ...
 - linux  crontab 命令
			
Linux 系统提供了使用者控制计划任务的命令 :crontab 命令. 一.crond简介 crond是linux下用来周期性的执行某种任务或等待处理某些事件的一个守护进程,与windows下的计划 ...
 - 每日一记:搭建Memcached + php 缓存系统
			
服务器环境,Centos6.5 1.安装Memcached服务端 Yum -y install memcached 2.配置Memcached服务端用户以及自动启动服务等 将服务配置成自启动 chkc ...
 - Ubuntu上部署C# 网站 步骤简单记录
			
对于刚接触linux的同学,由于命令不熟悉,所以要想在上面部署C#网站,容易迷茫,可以参考此简易步骤: 安装 mono: apt-get install mono 按tab搜索 找到mono相关的组 ...
 - js 获取 input file 文件 附给 image src
			
var a=document.querySelector('input[type=file]'); a.onchange = function (e) { //var reader = new Fil ...
 - 使用Docker解决同一服务器运行不同版本PHP方案。
			
前言: 最近公司有两个站点,分别是两种系统进行二次开发,基于LNMP架构的网站.一般想PHP这种非编译型语言想要对外出售源码都会进行加密,加密方法有很多种,大部分都是使用Zend Guard来进行加密 ...