android中LayoutInflater的用法及progressDialog的使用
在实际开发中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方法里面
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);//设置当前的布局为view
ProgressDialog的用法:
有两种:1.重写Activitiy里面的onCreateDialog,
但调用的时候,使用Activity的showDialog(0);
ProgressDialog pd = new ProgressDialog(this);
pd.setTitle("进度条");//设置标题
pd.setCancelable(true);//设置是否可以按回退键取消
pd.setIndeterminate(true);//设置为不明确
pd.setMessage("正在运行...");//设置内容
return pd;
}
2.直接创建出来
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//showDialog(0);
ProgressDialog.show(MainActivity.this, "进度条", "正在加载,请稍等...",true,true);
}
});