Android 为什么此警报对话框大小在某些设备上有效,而在某些设备上无效? 我想要的是:
当用户按下按钮时,我想提醒我想要的对话框位于右上角。我做了一些事情,它可以按照我想要的方式工作,但在某些设备上它不能按照我想要的方式工作。我的意思是,它的大小是错误的,当我在alertdialog外部单击时,它不会消失,我希望它在单击外部时消失 我的代码: 预期图像和错误图像:Android 为什么此警报对话框大小在某些设备上有效,而在某些设备上无效? 我想要的是:,android,android-alertdialog,android-windowmanager,Android,Android Alertdialog,Android Windowmanager,当用户按下按钮时,我想提醒我想要的对话框位于右上角。我做了一些事情,它可以按照我想要的方式工作,但在某些设备上它不能按照我想要的方式工作。我的意思是,它的大小是错误的,当我在alertdialog外部单击时,它不会消失,我希望它在单击外部时消失 我的代码: 预期图像和错误图像: 我建议您使用API-11及更高版本中提供的静态尺寸和X | Y参考,而不是使用AlertDialog在屏幕上指定静态尺寸和X | Y参考。对于较旧的API,您可以使用 例如,更多信息,请阅读和阅读 此外,如果下拉菜单应该
我建议您使用API-11及更高版本中提供的静态尺寸和X | Y参考,而不是使用
AlertDialog
在屏幕上指定静态尺寸和X | Y参考。对于较旧的API,您可以使用
例如,更多信息,请阅读和阅读
此外,如果下拉菜单应该是从ActionBar锚定的,那么我建议您坚持使用标准的菜单/组/项实现,而不是走艰苦的道路。您需要用自己的对话框主题覆盖设备的父对话框主题。谢谢您的回答,如何实现此菜单/组/项?这是一个很好的入门教程。但我没有使用动作栏。我使用linearlayout并对其进行自定义,这样示例就不适合我,那么我可以覆盖设备的父对话框主题吗?然后在我的答案中包含的链接上使用popupmenuclick
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);