自定义Dialog,动态设置宽高

自定义Dialog
public class ChangeModeDialog extends Dialog {

  public ChangeModeDialog(Context context) {
    super(context,R.style.Dialog);
    setCustomDialog();
}

private void setCustomDialog() {
    View mView = LayoutInflater.from(getContext()).inflate(R.layout.dialog_tip_layout, null);
    editText = (EditText) mView.findViewById(R.id.editText);
    positiveButton = (Button) mView.findViewById(R.id.positiveButton);
    negativeButton = (Button) mView.findViewById(R.id.negativeButton);
    super.setContentView(mView);
}
public View getEditText(){
    return editText;
}
@Override
public void setContentView(int layoutResID) {
}
@Override
public void setContentView(View view, ViewGroup.LayoutParams params) {
}
@Override
public void setContentView(View view) {
}
/**
 * * 确定键监听器
 * * @param listener
 * */
public void setOnPositiveListener(View.OnClickListener listener){
    positiveButton.setOnClickListener(listener);
}
/**
 * * 取消键监听器
 * * @param listener
 * */
public void setOnNegativeListener(View.OnClickListener listener){
    negativeButton.setOnClickListener(listener);
}
public void setTitle(String t)
{
    title.setText(t);
}
public void setpositiveButtonTxt(String t)
{
    positiveButton.setText(t);
}
public void setnegativeButtonTxt(String t)
{
    negativeButton.setText(t);
}

}

//代码中动态设置宽高

public void onChangeControlModeOnClick(View view) {

    final ChangeModeDialog dialog = new ChangeModeDialog(this);
    dialog.setOnPositiveListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String pwd = ((EditText) dialog.getEditText()).getText().toString();
            if (StringUtil.isEmpty(pwd)) {
                Toast.makeText(UnderDeviceDetailActivity.this, "请输入密码!", Toast.LENGTH_SHORT).show();
            } else {
                getMessenger().sendRecoveryDeviceToControlledMode(deviceId, pwd);
                dialog.dismiss();
            }
        }
    });
    dialog.setOnNegativeListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            dialog.dismiss();
        }
    });
    dialog.show();

    int screenWidth1 = this.getWindowManager().getDefaultDisplay().getWidth();
    if (screenWidth1 >= 450) {  //大屏幕,直接写死dialog的宽度
        WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
        params.width = dp2px(450);

        dialog.getWindow().setAttributes(params);
    } else {
        //小屏幕   宽度手机两边留空格
        WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
        params.width = this.getWindowManager().getDefaultDisplay().getWidth() - dp2px(40);
        dialog.getWindow().setAttributes(params);
    }
}

你可能感兴趣的:(自定义Dialog,动态设置宽高)