Android:自定义Dialog大小,显示圆角
经过测试,可以使用.
-----------------------------------------------------------
AlertDialog.Builder builder = new AlertDialog.Builder(this); AlertDialog dialog = builder.create(); int screenWidth_px = PX_DP_utils.getScreenWidth_PX(this); int screenHeight_px = PX_DP_utils.getScreenheight_PX(this); dialog.show(); //需要设置属性,否则dialog的大小不起作用!必须先show再set属性 WindowManager.LayoutParams params = dialog.getWindow().getAttributes(); params.width = (int) (screenWidth_px * 0.7 + 0.5f); params.height = (int) (0.6 * screenHeight_px + 0.5f);
//设置位置的属性
Window dialogWindow = dialog.getWindow();
WindowManager.LayoutParams lp = dialogWindow.getAttributes();
dialogWindow.setGravity(Gravity.LEFT | Gravity.TOP);
/*
* lp.x与lp.y表示相对于原始位置的偏移.
* 当参数值包含Gravity.LEFT时,对话框出现在左边,所以lp.x就表示相对左边的偏移,负值忽略.
* 当参数值包含Gravity.RIGHT时,对话框出现在右边,所以lp.x就表示相对右边的偏移,负值忽略.
* 当参数值包含Gravity.TOP时,对话框出现在上边,所以lp.y就表示相对上边的偏移,负值忽略.
* 当参数值包含Gravity.BOTTOM时,对话框出现在下边,所以lp.y就表示相对下边的偏移,负值忽略.
* 当参数值包含Gravity.CENTER_HORIZONTAL时
* ,对话框水平居中,所以lp.x就表示在水平居中的位置移动lp.x像素,正值向右移动,负值向左移动.
* 当参数值包含Gravity.CENTER_VERTICAL时
* ,对话框垂直居中,所以lp.y就表示在垂直居中的位置移动lp.y像素,正值向右移动,负值向左移动.
* gravity的默认值为Gravity.CENTER,即Gravity.CENTER_HORIZONTAL |
* Gravity.CENTER_VERTICAL.
*
* 本来setGravity的参数值为Gravity.LEFT | Gravity.TOP时对话框应出现在程序的左上角,但在
* 我手机上测试时发现距左边与上边都有一小段距离,而且垂直坐标把程序标题栏也计算在内了,
* Gravity.LEFT, Gravity.TOP, Gravity.BOTTOM与Gravity.RIGHT都是如此,据边界有一小段距离
*/
lp.x = 100; // 新位置X坐标
lp.y = 100; // 新位置Y坐标
lp.width = 300; // 宽度
lp.height = 300; // 高度
lp.alpha = 0.7f; // 透明度
// 当Window的Attributes改变时系统会调用此函数,可以直接调用以应用上面对窗口参数的更改,也可以用setAttributes
// dialog.onWindowAttributesChanged(lp);
dialogWindow.setAttributes(lp);
Window dialogWindow = dialog.getWindow();dialogWindow.getDecorView().setPadding(0, 0, 0, 0);//去除边框
// 必须使用这个方法,不能使用dialog.setView()的方法 dialog.getWindow().setContentView(R.layout.dialog_picpick_three); //设置dialog的背景颜色为透明色,就可以显示圆角了!! dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); dialog.getWindow().setAttributes(params);
//------------------------------------EditText相关代码,能够获取到焦点,能够直接弹出软键盘.
//解决dilaog中EditText无法弹出输入的问题
dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
//弹出对话框后直接弹出键盘
et_newReason.setFocusableInTouchMode(true);
et_newReason.requestFocus();
CmzBossApplication.handler.postDelayed(new Runnable() {
@Override
public void run() {
InputMethodManager inputManager =(InputMethodManager) et_newReason.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.showSoftInput(et_newReason, 0);
}
}, 100);
//---------------------------解决RecycleView下的matchparent不好用的问题
class MyRVAdapter extends RecyclerView.Adapter<MyRVAdapter.VH> {
@Override
public VH onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.exchangeveg_reason_item, parent, false);//必须用这种方式来填充View
return new VH(view);
}
@Override
public void onBindViewHolder(VH holder, final int position) {
holder.tv_reason.setText(exchange_veg_reasons.get(position).reason);
holder.iv_reason_reduce.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//删除本行的ITEM
String reason_id = exchange_veg_reasons.get(position).id;
HttpCashManger.getInstance().postExchangeReduceReason(reason_id, new HttpCallback() {
@Override
public void onSuccess(String result) {
System.out.println("删除备注信息成功:" + result);
exchange_veg_reasons.remove(exchange_veg_reasons.get(position));
myRVAdapter.notifyDataSetChanged();
}
@Override
public void onFail(Exception e) {
}
});
}
});
}
@Override
public int getItemCount() {
return exchange_veg_reasons.size();
}
class VH extends RecyclerView.ViewHolder {
public TextView tv_reason;
public ImageView iv_reason_reduce;
public VH(View itemView) {
super(itemView);
tv_reason = (TextView) itemView.findViewById(R.id.ecr_tuicai_tv_item_reason);
iv_reason_reduce = (ImageView) itemView.findViewById(R.id.ecr_tuicai_iv_item_reaosn_reduce);
}
}
}