当用户按下按钮我想要警告我想要的右上角。我做了一些事情,它按照我想要的方式工作,但在某些设备上它不能按我的意愿工作。我的意思是它的大小是错误的,当我点击警报对话外并没有消失时,我希望它在我点击外面时消失。
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("");
builder.setCancelable(true);
builder.setItems(items , new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
switch (item) {
case 0:
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
MainActivity.this);
// set title
alertDialogBuilder.setTitle(areusure);
alertDialogBuilder.setCancelable(true);
// set dialog message
alertDialogBuilder
.setMessage(clicktologout)
.setCancelable(true)
.setPositiveButton( yes,new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
//something
}
})
.setNegativeButton(no_,new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
break;
case 1:
try {
Intent myIntent = new Intent(MainActivity.this,tutorial.class);
startActivity(myIntent);
} catch (Exception e) {
Toast toast= Toast.makeText(MainActivity.this,
feedus, Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER|Gravity.CENTER_HORIZONTAL, 0, 0);
toast.show();
}
break;
case 2:
Intent myIntent = new Intent(MainActivity.this,about.class);
startActivity(myIntent);
break;
}
}
});
AlertDialog alert = builder.create();
alert.requestWindowFeature(Window.FEATURE_NO_TITLE);
alert.setCancelable(true);
WindowManager.LayoutParams wmlp = alert.getWindow().getAttributes();
wmlp.gravity = Gravity.TOP | Gravity.RIGHT;
alert.show();
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(alert.getWindow().getAttributes());
int value_y = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
(float) 42, getResources().getDisplayMetrics());
int value_x = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
(float) 20, getResources().getDisplayMetrics());
lp.width = 500;
lp.height = 500;
lp.x=-value_x;
lp.y=value_y;
alert.getWindow().setAttributes(lp);
预期图片和错误图片:
答案 0 :(得分:2)
我建议您使用API-11及更高版本中提供的PopupMenu
,而不是使用AlertDialog
并在屏幕上指定静态尺寸和X | Y引用。对于较旧的API,您可以使用support-v7 library。
此外,如果下拉菜单应该从ActionBar锚定,那么我建议你坚持使用标准的菜单/组/项实现,而不是艰难前行。