展会信息港展会大全

LayoutInflater自定义布局 Android开发教程
来源:互联网   发布日期:2015-10-13 14:55:22   浏览:1692次  

导读:LayoutInflater的作用类似于 findViewById(),不同点是LayoutInflater是用来找layout文件夹下的xml布局文件,并且实例化!而 findViewById()是找具体某一个xml下的具体 widget控件(如:Button,TextView等)。自定......

LayoutInflater的作用类似于 findViewById(),不同点是LayoutInflater是用来找layout文件夹下的xml布局文件,并且实例化!而 findViewById()是找具体某一个xml下的具体 widget控件(如:Button,TextView等)。

自定义Dialog时,通过LayoutInflater实例化layout文件夹下的布局文件的方法有2种。

第1种:

case R.id.button4:

LayoutInflater inflater = (LayoutInflater)getApplicationContext()

.getSystemService(LAYOUT_INFLATER_SERVICE);

View view = inflater.inflate(R.layout.course_custom_dialog, null);

AlertDialog.Builder builder2=new AlertDialog.Builder(MainDialogActivity.this);

builder2.setView(view);

builder2.setTitle("添加课程")

.setPositiveButton("保存", new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

dialog.cancel();

}

})

.setNegativeButton("取消", null)

.create().show();

break;

第2种:

case R.id.button4:

LayoutInflater inflater = (LayoutInflater)LayoutInflater.from(getApplicationContext());

View view = inflater.inflate(R.layout.course_custom_dialog,null);

AlertDialog.Builder builder2=new AlertDialog.Builder(MainDialogActivity.this);

builder2.setView(view);

builder2.setTitle("添加课程")

.setPositiveButton("保存", new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

dialog.cancel();

}

})

.setNegativeButton("取消", null)

.create().show();

break;

赞助本站

人工智能实验室

相关热词: LayoutInflater

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港