我正在开发一个 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。所以它的宽度会太小。但这就是我得到的。
如何设置自定义提醒对话框的宽度?
最佳答案
您可以通过多种方式控制它。给大家分享一种方法,设置告警窗口的宽高。
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/