弹出框在安卓开发中是经常会用到的,如果单纯的只是用安卓自带的对话框模式肯定是十分单调的。
再来我看来安卓一定有一个功能让我们像Layout添加Layout一样把Layout添加到提示框里面。
所以忘了搜了一下,终于找到了一种不错的方式,在此实验并记录一下。
至于实在activity里面定义还是在实现Layout的类中定义都是可以的
添加如下方法,在监听时间里面直接调用该方法就可以了。
private void openOptionsDialog() {
// 生成对话框
AlertDialog dlg = new AlertDialog.Builder((Context)activity).create();
//显示对框框
dlg.show();
Window window = dlg.getWindow();
//添加自定义的Layout以及布局方式,注意传入dlg对象本身方便关闭该提示框
window.addContentView(new MyDialog(activity,dlg),new LayoutParams(-1, -1));
}
然后定义一个弹出框,集成RelativeLayout
public class MyDialog extends RelativeLayout{
private AlertDialog dlg;
private Context context;
private Button canel;
public MyDialog(Context context,AlertDialog dlg) {
super(context);
this.context=context;
// TODO Auto-generated constructor stub
this.dlg=dlg;
init();
}
private void init() {
//这里可以任意的写入一些控件
//添加取消按钮
canel=new Button(context);
canel.setText("×");
LayoutParams lp=new LayoutParams(10,10);
lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
addView(canel, lp);
canel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("取消弹出框");
dlg.cancel();
}
});
}
}然后就大功告成了。