针对自定义Dialog对话框(可以参考之前的博客)没有按设置的宽度显示的问题,可以采用如下方式解决:
Window window = dialog.getWindow();
WindowManager.LayoutParams lp = window.getAttributes();
//将宽度值dp转px
lp.width = context.getResources().getDimensionPixelOffset(R.dimen.dp_210);
//高度自适应(也可设置为固定值,同宽度设置方法)
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
dialog.getWindow().setAttributes(lp);
即在之前自定义dialog的create()中添加以上代码即可.完整代码如下所示:
public ResultDisplayDialog create() {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// instantiate the dialog with the custom Theme
final ResultDisplayDialog dialog = new ResultDisplayDialog(context, R.style.Dialog);
dialog.setCanceledOnTouchOutside(false);
View layout = inflater.inflate(R.layout.layout_result_display_dialog, null);