Android中自定义Dialog样式

转载:Android中自定义Dialog样式
dialog代码

public class MyMiddleDialog extends Dialog {
    private Context context;
    public MyMiddleDialog(Context context) {
        super(context);
    }
    public MyMiddleDialog(Context context, int themeResId) {
        super(context, themeResId);
        this.context = context;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.register_y_d, null);
        this.setContentView(layout);
    }
}

dialog样式


调用dialog代码

//设置样式
                MyMiddleDialog myMiddleDialog = new MyMiddleDialog(getActivity(), R.style.MyMiddleDialogStyle);
                Window window = myMiddleDialog.getWindow();
                //设置边框距离
                window.getDecorView().setPadding(0, 0, 0, 0);
                //设置dialog位置
                window.setGravity(Gravity.RIGHT|Gravity.TOP);
                WindowManager.LayoutParams lp = window.getAttributes();
                //设置宽高
                lp.width = WindowManager.LayoutParams.WRAP_CONTENT;   
                lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
                window.setAttributes(lp);
                //设置点击Dialog外部任意区域关闭Dialog
                myMiddleDialog.setCanceledOnTouchOutside(true);
                myMiddleDialog.show();

你可能感兴趣的:(Android)