java - 如何在android中设置自定义对话框的高度和宽度

标签 java android

如何将此对话框的自定义高度和宽度直接设置到主 Activity 文件中。我尝试了一种方法,但它只匹配父级的高度和宽度

public void onBackPressed()
    {

        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setTitle(R.string.app_name);
        builder.setIcon(R.mipmap.ic_launcher);
        builder.setMessage("Please Help Us Grow before leaving!")
                .setCancelable(false)
     .setPositiveButton("Exit", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        finish();
                    }
                })
                .setNegativeButton("Share our app", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {

                        Intent sendIntent = new Intent(); sendIntent.setAction(Intent.ACTION_SEND);
                        sendIntent.putExtra(Intent.EXTRA_TEXT, "Download it now: https://play.google.com/store/apps/details?id=com.app.package");
                        sendIntent.setType("text/plain");
                        sendIntent.setPackage("com.whatsapp");
                        startActivity(sendIntent);
                    }
                });
        AlertDialog alertDialog = builder.create();
        alertDialog.show();
        WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
        layoutParams.copyFrom(alertDialog.getWindow().getAttributes());
        layoutParams.width = WindowManager.LayoutParams(100);
        layoutParams.height = WindowManager.LayoutParams(100);
        alertDialog.getWindow().setAttributes(layoutParams);

最佳答案

您可以将其设置为alertDialog.getWindow().setLayout(600, 400);也可以将其设置为

    WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
    layoutParams.copyFrom(alertDialog.getWindow().getAttributes());
    layoutParams.width = 150;
    layoutParams.height = 500;
    layoutParams.x=-180;
    layoutParams.y=100;
    alertDialog.getWindow().setAttributes(layoutParams);

关于java - 如何在android中设置自定义对话框的高度和宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62421473/

相关文章:

java - 使用 Guice,如何注入(inject)有界通配符类?

java - 在 Java 中,为什么要将括号放在数组名称的前面?

java - 在android中将float转换为CharSequence?

android - 尝试在空对象上调用虚拟方法boolean RecyclerView $ ViewHolder shouldIgnore()

android - 实时对接学习路径

android - 如何在android DCIM中创建一个新文件夹

android - 打开 Assets 数据库文件时出现 java.lang.NullPointerException

java - 将数组的随机字母保存为字符串

java - RoboFragment 不适用于 FragmentTransaction.add(int,Fragment) 方法

android - 当我从一个 fragment 回到另一个 fragment 时,我的观察者总是开火