展会信息港展会大全

android中LayoutInflater的用法及progressDialog的使用
来源:互联网   发布日期:2015-10-03 10:58:12   浏览:1848次  

导读:在实际开发中LayoutInflater这 个类还是非常有用的,它的作用类似于findViewById()。不同点是LayoutInflater是用来找res/layout/下的xml布局 文件,并且实例化;而findViewById()是找xml布局文件下的具体widge......

在实际开发中LayoutInflater这 个类还是非常有用的,它的作用类似于findViewById()。不同点是LayoutInflater是用来找res/layout/下的xml布局 文件,并且实例化;而findViewById()是找xml布局文件下的具体widget控件(如Button、TextView等)。

具体作用:

1、对于一个没有被载入或者想要动态载入的界面,都需要使用LayoutInflater.inflate()来载入;

2、对于一个已经载入的界面,就可以使用Activiyt.findViewById()方法来获得其中的界面元素。

在使用时候有三种方法:

//LayoutInflater inflater = LayoutInflater.from(context);

//LayoutInflater inflater=getLayoutInflater();

LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);

请看下面的详细代码:注意在onCreate方法里面

Button btn =new Button( this);

btn.setText("点击加载");

// LayoutInflater inflater = LayoutInflater.from(this);

// LayoutInflater inflater=getLayoutInflater();

LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);

RelativeLayout view = (RelativeLayout) inflater.inflate(R.layout.activity_main,null);

view.addView(btn);

setContentView(view);//设置当前的布局为 vi ew

ProgressDialog的用法:

有两种:1.重写Activitiy里面的onCreateDialog,

但调用的时候,使用Activity的showDialog(0);

public Dialog onCreateDialog( int id) {

ProgressDialog pd =new ProgressDialog( this);

pd.setTitle("进度条"); // 设置标题

pd.setCancelable( true); // 设置是否可以按回退键取消

pd.setIndeterminate( true); // 设置为不明确

pd.setMessage("正在运行..."); // 设置内容

return pd;

}

2.直接创建出来

btn.setOnClickListener( new OnClickListener() {

@Override

publicvoid onClick(View v) {

//TODO Auto-generated method stub

// showDialog(0);

ProgressDialog.show(MainActivity. this, "进度条", "正在加载,请稍等...", true, true);

}

});

赞助本站

人工智能实验室

相关热词: LayoutInflater progressDialog

相关内容
AiLab云推荐
展开

热门栏目HotCates

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