Context.LAYOUT_INFLATER_SERVICE的使用
来源:互联网 发布:网络整合营销理论4i 编辑:程序博客网 时间:2024/05/23 02:28
在实际开发种LayoutInflater这个类还是非常有用的,它的作用类似于 findViewById(),
不同点是LayoutInflater是用来找layout下xml布局文件,并且实例化!而findViewById()是找具体xml下的具体 widget控件.
下面是一个简单的Demo,主布局main.xml里有一个TextView和一个Button,当点击Button,出现 Dialog,而这个Dialog的布局方式是我们在layout目录下定义的custom_dialog.xml文件(里面左右分布,左边 ImageView,右边TextView)。
代码如下:
package com.bivin;
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();
}
}
阅读全文
0 0
- Context.LAYOUT_INFLATER_SERVICE的使用
- context.getSystemService(context.LAYOUT_INFLATER_SERVICE)调用时报NullPointerException
- Context使用的技巧
- Context的使用
- Context的使用
- android Context的使用
- Context的使用(Using Context)
- android的context使用,工具类context
- Context——Context的使用
- <context:component-scan> 的使用
- context与getApplication()的使用
- android中context的使用
- hadoop的Context简单使用
- Android中Context的使用
- React 中 context 的使用
- golang Context包的使用
- Android里Context的使用
- Android里Context的使用
- CentOS 7升级python
- 2017年,我们为什么要学习C++?
- Java并发编程:volatile关键字解析
- STM32-USART
- json与jsonp之间的区别
- Context.LAYOUT_INFLATER_SERVICE的使用
- gcc -m32选项
- 一维数组,二维数组,三维数组名的含义
- 华硕B250主板安装Ubuntu16.04+gtx1050Ti显卡驱动+cuda8.0+cudnn5.1+opencv3.1+caffe+tensorflow
- Array 的copyWithin() 方法
- linux下freetds+unixodbc 连接sql server
- LVS原理与使用(5)
- react-native运行ios报错总结
- 世峰数字与高露洁达成企业宣传项目合作