經過測試,可以使用.
-----------------------------------------------------------
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);
}
}
}
