最近开发软件的时候,需要实现一个在底部显示的dialog,入下图所示:
来看看这个dialog的创建:
switch_camera_dialog = new Dialog(getActivity());
switch_camera_dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
switch_camera_dialog.setContentView(R.layout.switch_camera_dialog);
switch_camera_dialog.setCanceledOnTouchOutside(true);
//switch_camera_dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_STATUS_BAR_PANEL);
Window dialogWindow = switch_camera_dialog.getWindow();
WindowManager.LayoutParams lp = dialogWindow.getAttributes();
//dialogWindow.setGravity(Gravity.LEFT | Gravity.TOP);
lp.width = LayoutParams.MATCH_PARENT;
lp.height = LayoutParams.WRAP_CONTENT;
lp.gravity = Gravity.BOTTOM;
dialogWindow.setAttributes(lp);
//dialogWindow.requestFeature(Window.FEATURE_NO_TITLE);
dialogWindow.setBackgroundDrawableResource(R.drawable.fingerprint_bg_white);
View_camera_before = (View)switch_camera_dialog.findViewById(R.id.icon_camera_Before);
View_camera_before.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
selected_san_pai_camera_switch = FingerPrint_Utils.shanpai_pcakage_before;
//saved = true;
View_camera_before.setSelected(true);
View_camera_back.setSelected(false);
}});
View_camera_back = (View)switch_camera_dialog.findViewById(R.id.icon_camera_Back);
View_camera_back.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
selected_san_pai_camera_switch = FingerPrint_Utils.shanpai_pcakage_back;
//saved = true;
View_camera_before.setSelected(false);
View_camera_back.setSelected(true);
}});
if(selected_str != null && selected_str.indexOf(FingerPrint_Utils.shanpai_pcakage_before,0) > -1){
View_camera_before.setSelected(true);
}else{
View_camera_back.setSelected(true);
}
Button ok_button = (Button) switch_camera_dialog.findViewById(R.id.button_ok); //
ok_button.setBackgroundResource(hwdroid.R.drawable.hw_btn_default);
ok_button.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
if(selected_san_pai_camera_switch == null){
selected_san_pai_camera_switch = FingerPrint_Utils.shanpai_pcakage_back;
}
if(selected_Preference != null){
selected_Preference.setChecked(false);
}
selected_Preference = SHAN_PAI_Preference;
selected_str = selected_san_pai_camera_switch;
SHAN_PAI_Preference.setChecked(true);
switch_camera_dialog.dismiss();
selected_san_pai_camera_switch = null;
saved = true;
}});
Button no_button = (Button) switch_camera_dialog.findViewById(R.id.button_cancel);
no_button.setBackgroundResource(hwdroid.R.drawable.hw_btn_default);
no_button.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
selected_san_pai_camera_switch = null;
if(selected_Preference != null){
selected_Preference.setChecked(true);
}
//SHAN_PAI_Preference.setChecked(false);
switch_camera_dialog.dismiss();
}});
//switch_camera_dialog.setOnDismissListener(this);
其中重点代码:
switch_camera_dialog.setContentView(R.layout.switch_camera_dialog);
//.....
Window dialogWindow = switch_camera_dialog.getWindow();
WindowManager.LayoutParams lp = dialogWindow.getAttributes();
//dialogWindow.setGravity(Gravity.LEFT | Gravity.TOP);
lp.width = LayoutParams.MATCH_PARENT;
lp.height = LayoutParams.WRAP_CONTENT;
lp.gravity = Gravity.BOTTOM;
dialogWindow.setAttributes(lp);