我想为一个按钮添加onclicklistener,我想在单击按钮时在屏幕左侧显示对话框。我试图实现它,但它总是出现在屏幕的中心。知道如何实现它吗?
最佳答案
如果我正确理解你的问题-你想对齐你的对话框不出现在屏幕的中心。然后看看这个代码示例。
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
if(item == 0) {
} else if(item == 1) {
} else if(item == 2) {
}
}
});
AlertDialog dialog = builder.create();
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
WindowManager.LayoutParams WMLP = dialog.getWindow().getAttributes();
WMLP.x = 100; //x position
WMLP.y = 100; //y position
dialog.getWindow().setAttributes(WMLP);
dialog.show();
这里x位置的值是从左到右的像素。因为y位置值是从下到上的。
关于android - 如何使对话框出现在左侧?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7607172/