gpt4 book ai didi

android - 如何在 Android 中将 AlertDialog 的位置设置在屏幕顶部之外?

转载 作者:太空宇宙 更新时间:2023-11-03 10:32:00 24 4
gpt4 key购买 nike

我想把AlertDialog的位置设置在状态栏后面,当我的Dialog中的内容会增加的时候,怎么办?我正在使用我自己的布局创建自定义 AlertDialog....请帮助我....


下面是我的代码,我正在设置 alertDialog 的高度和 x-y 位置,但它仍然没有显示它的效果..

AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inf = getLayoutInflater();
View layout = inf.inflate(R.layout.main, null);
builder.setView(layout);
builder.setTitle("Add to Home screen");
AlertDialog dialog = builder.create();
WindowManager.LayoutParams WMLP = dialog.getWindow().getAttributes();
int dialogOriginalHeight = WMLP.height;
WMLP.height += 750;
Log.i("XnY", "x="+WMLP.x+", y="+WMLP.y);
WMLP.x = -10; //x position
WMLP.y = -10; //y position
Log.i("XnY", "x="+WMLP.x+", y="+WMLP.y);
dialog.getWindow().setAttributes(WMLP);
Log.i("POSITION", "POS::HEIGHT:"+WMLP.height);
dialog.show();

最佳答案

我知道这真的很老,但对于以后看到它的人来说:

x 和 y 被忽略,因为您使用的是默认重力。

来自docs :

X position for this window. With the default gravity it is ignored. When using LEFT or START or RIGHT or END it provides an offset from the given edge.

我需要从右上角偏移我的重力,所以我将重力设置为 0x00000035。这是顶部和右侧。 (顶部是 0x00000030,右边是 0x00000005)。这将导致 x 和 y 不被忽略。

alert.getWindow().setGravity(0x00000035);

关于android - 如何在 Android 中将 AlertDialog 的位置设置在屏幕顶部之外?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3160778/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com