WindowManager winManage = getWindowManager() ;
Display winDisplay = winManage.getDefaultDisplay() ;
int winHeight = winDisplay.getHeight() ;
int winWeight = winDisplay.getWidth() ;
获取屏幕的长度和宽度,这个在Dialog中同样适用的
// 自定义对话框
class IdentikitDialogs extends Dialog implements android.view.View.OnClickListener {
private Button b1;
private Window window = null;
private TextView tvMsg ;
private String msg ;
public IdentikitDialogs(Context context, String msg) {
super(context);
this.msg = msg ;
}
public void setDisplay() {
setContentView(R.layout.identikit_dialog);// 设置对话框的布局
tvMsg = (TextView)this.findViewById(R.id.tv_identikit_action) ;
tvMsg.setGravity(Gravity.CENTER) ;
tvMsg.setText(msg) ;
b1 = (Button) findViewById(R.id.clo);
b1.setOnClickListener(this);
setProperty();
//setTitle("自定义对话框");// 设定对话框的标题
show();// 显示对话框
}
// 要显示这个对话框,只要创建该类对象.然后调用该函数即可.
public void setProperty() {
WindowManager winManage = getWindowManager() ;
Display winDisplay = winManage.getDefaultDisplay() ;
int winHeight = winDisplay.getHeight() ;
int winWeight = winDisplay.getWidth() ;
window = getWindow();// 得到对话框的窗口.
WindowManager.LayoutParams wl = window.getAttributes();
wl.x = 0;// 这两句设置了对话框的位置.0为中间
wl.y = 180;
wl.alpha = 0.6f;// 这句设置了对话框的透明度
// wl.gravity = Gravity.BOTTOM;
wl.height = winHeight ;
wl.width = winWeight ;
wl.gravity = Gravity.TOP ;
window.setAttributes(wl);
}
@Override
public void onClick(View v) {
dismiss();// 取消
}
} //end class
其中 setContentView(R.layout.identikit_dialog);// 设置对话框的布局
是自己在xml文件中定义的布局