java - 无法在 Android 中设置自定义对话框的宽度

标签 java android android-layout android-alertdialog

我正在开发一个 Android 应用程序。在我的应用程序中,我试图显示一个警报对话框设置自定义 View 。设置自定义 View 就可以了。但是我在设置对话框的宽度时遇到问题。我无法设置宽度。它总是默认的。

这是我显示对话框的方式

AlertDialog.Builder builder = new AlertDialog.Builder(context);
            View view = layoutInflater.inflate(R.layout.meme_post_actions_dialog,null);
            builder.setView(view);
            builder.create().show();

这是xml布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:gravity="center"
    android:orientation="vertical" android:layout_width="100dp"
    android:layout_height="match_parent">
    <TextView
        android:text="This is dialog"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

如您所见,我将宽度设置为 100dp。所以它的宽度会太小。但这就是我得到的。

enter image description here

如何设置自定义提醒对话框的宽度?

最佳答案

您可以通过多种方式控制它。给大家分享一种方法,设置告警窗口的宽高。

AlertDialog.Builder builder = new AlertDialog.Builder(this);
View view = layoutInflater.inflate(R.layout.meme_post_actions_dialog,null);
        builder.setView(view);
builder.setView(layout);
alertDialog = builder.create();
alertDialog.show();
alertDialog.getWindow().setLayout(600, 400); //<--Controlling width and height.

最后确保布局的父级应与父级匹配。

android:layout_width="match_parent"
android:layout_height="match_parent"

关于java - 无法在 Android 中设置自定义对话框的宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40055504/

相关文章:

java - 从 mysql 查询返回时出现空指针异常

java - Android java.time.LocalDateTime

android布局按钮看起来不像预期的那样

java - 安卓 : Adding close button into achartengine chart layout

java - 将 JTabbedpane 拖出到窗口并自动创建新的 JFrame

java - 我的 DDD 值对象中是否隐藏了 DDD 实体?

android - LinearLayout findViewById 问题 - 崩溃

Android CookieManager setCookie 没有设置任何东西

java - View 中的空指针

android - viewBinding为什么让XML根全屏