gpt4 book ai didi

android - 如何在 android 中对齐自定义对话框中心?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:41:27 24 4
gpt4 key购买 nike

我正在开发我想将对话框显示为屏幕大小的应用程序。所以我使用了下面的代码。我通过这里得到了解决方案 Alert message is not displaying in alert dialog box?

 AlertDialog.Builder builder = new AlertDialog.Builder(this);
TextView title = new TextView(this);
title.setText("DM2");
title.setBackgroundColor(Color.DKGRAY);
title.setPadding(10, 10, 10, 10);
title.setGravity(Gravity.CENTER);
title.setTextColor(Color.WHITE);
title.setTextSize(20);


TextView text = new TextView(this);
text.setText("Hello This text");
text.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
text.setTextSize(20);
text.setGravity(Gravity.CENTER);

//Creates a linearlayout layout and sets it with initial params
LinearLayout ll = new LinearLayout(this);
ll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
ll.setGravity(Gravity.CENTER);
ll.addView(text);
builder.setCustomTitle(title);
builder.setPositiveButton(
"Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});

Dialog d = builder.setView(ll).create();
//Fills up the entire Screen
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(d.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.FILL_PARENT;
lp.height = WindowManager.LayoutParams.FILL_PARENT;
d.show();
d.getWindow().setAttributes(lp);

但我希望对话框居中对齐。现在它显示在窗口的顶部。我尝试使用 lp.garvity = Gravity.center,但没有用。如果设备方向发生变化,我需要多按 ok 按钮以关闭对话框。如何解决这个问题?

提前致谢推帕

最佳答案

我迟到了一点,但迟到总比不迟到好:-)你可以使用下面的代码:(我也看到了 here )

dialog = new Dialog(activity, android.R.style.Theme_Translucent_NoTitleBar);

dialog.setContentView(R.layout.activity_upload_photo_dialog);

Window window = dialog.getWindow();
window.setLayout(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
window.setGravity(Gravity.CENTER);

//The below code is EXTRA - to dim the parent view by 70%
LayoutParams lp = window.getAttributes();
lp.dimAmount = 0.7f;
lp.flags = LayoutParams.FLAG_DIM_BEHIND;
dialog.getWindow().setAttributes(lp);
//Show the dialog
dialog.show();

希望我帮到了...

关于android - 如何在 android 中对齐自定义对话框中心?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10447410/

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