如何构建具有多行标题的警报对话框?

发布于 2025-01-01 14:08:05 字数 110 浏览 2 评论 0原文

Android 警报对话框中是否可以有多行标题?我尝试了这里发布的几个解决方案,但没有一个对我有用。我总是以显示 3 个点 (...) 字符串的标题结束。 任何与此相关的示例代码或工作示例都将受到高度赞赏。

Is it possible to have a multi-line title in an Android alert dialog? I tried a couple of solutions posted here but none worked for me. I always end up with the title showing 3 dots (...) string for title.
Any sample code or working example regarding the same would be highly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

幽蝶幻影 2025-01-08 14:08:05

您需要使用 builder.setCustomTitle():

AlertDialog.Builder builder = new AlertDialog.Builder(context);
TextView textView = new TextView(context);
textView.setText("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur " +
                "tincidunt condimentum tristique. Vestibulum ante ante, pretium porttitor " +
                "iaculis vitae, congue ut sem. Curabitur ac feugiat ligula. Nulla " +
                "tincidunt est eu sapien iaculis rhoncus. Mauris eu risus sed justo " +
                "pharetra semper faucibus vel velit.");
builder.setCustomTitle(textView);

文档位于: AlertDialog.builder

“进入图片描述在这里"

You need to use builder.setCustomTitle():

AlertDialog.Builder builder = new AlertDialog.Builder(context);
TextView textView = new TextView(context);
textView.setText("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur " +
                "tincidunt condimentum tristique. Vestibulum ante ante, pretium porttitor " +
                "iaculis vitae, congue ut sem. Curabitur ac feugiat ligula. Nulla " +
                "tincidunt est eu sapien iaculis rhoncus. Mauris eu risus sed justo " +
                "pharetra semper faucibus vel velit.");
builder.setCustomTitle(textView);

Documentation is here: AlertDialog.builder

enter image description here

羁〃客ぐ 2025-01-08 14:08:05

在我看来,这是一个更干净的解决方案。

对话框主题:

<style name="CustomDialogTheme" parent="ThemeOverlay.MaterialComponents.Dialog.Alert">
    <item name="materialAlertDialogTitleTextStyle">@style/TitleStyle</item>
</style>

标题样式:

<style name="TitleStyle" parent="@style/MaterialAlertDialog.MaterialComponents.Title.Text">
    <item name="android:maxLines">3</item>
    <item name="android:singleLine">false</item>
</style>

在代码中:

MaterialAlertDialogBuilder(context, R.style.CustomDialogTheme)
    .setTitle("Long title...")
    .show()

PS:您还可以指定不同的数字,不一定是 3。或者尝试您想要的样式。

It seems to me that this is a cleaner solution.

Dialog theme:

<style name="CustomDialogTheme" parent="ThemeOverlay.MaterialComponents.Dialog.Alert">
    <item name="materialAlertDialogTitleTextStyle">@style/TitleStyle</item>
</style>

Title style:

<style name="TitleStyle" parent="@style/MaterialAlertDialog.MaterialComponents.Title.Text">
    <item name="android:maxLines">3</item>
    <item name="android:singleLine">false</item>
</style>

In code:

MaterialAlertDialogBuilder(context, R.style.CustomDialogTheme)
    .setTitle("Long title...")
    .show()

PS: You can also specify a different number, not necessarily 3. Or play around with the styles you want.

鹤舞 2025-01-08 14:08:05

这是设置标题的方法

AlertDialog.Builder builder = new  AlertDialog.Builder(Class name.this);
    builder.setTitle("Welcome to App,\n There are no App.\n Add a new data.");

This is the way to set title

AlertDialog.Builder builder = new  AlertDialog.Builder(Class name.this);
    builder.setTitle("Welcome to App,\n There are no App.\n Add a new data.");
衣神在巴黎 2025-01-08 14:08:05

您可以在 onShowListener 中通过 ID 搜索 titleView:

val dialog = AlertDialog.Builder(this)
    .setTitle(R.string.consent_dalog_title)
    .setMessage(getString(R.string.consent_dialog_message))
    .setCancelable(false)
    .setPositiveButton(getString(R.string.consent_dialog_permit)) { dialog, _ ->
        dialog.cancel()
    }
    .setNegativeButton(getString(R.string.constent_dialog_deny)) { dialog, _ ->
        dialog.cancel()
    }
    .create()
    dialog.setOnShowListener {
        dialog.findViewById<TextView>(R.id.alertTitle)?.let {
            it.maxLines = 5
        }
    }
    dialog.show()

You can search the titleView by its ID within the onShowListener:

val dialog = AlertDialog.Builder(this)
    .setTitle(R.string.consent_dalog_title)
    .setMessage(getString(R.string.consent_dialog_message))
    .setCancelable(false)
    .setPositiveButton(getString(R.string.consent_dialog_permit)) { dialog, _ ->
        dialog.cancel()
    }
    .setNegativeButton(getString(R.string.constent_dialog_deny)) { dialog, _ ->
        dialog.cancel()
    }
    .create()
    dialog.setOnShowListener {
        dialog.findViewById<TextView>(R.id.alertTitle)?.let {
            it.maxLines = 5
        }
    }
    dialog.show()
久而酒知 2025-01-08 14:08:05

如果您使用警报对话框,则标题最多可以包含 2 行,否则您必须使用自定义对话框。

If you are using alert dialog then title can contain maximum 2 line, else you have to go with custom Dialog.

愚人国度 2025-01-08 14:08:05

实际上,这里的“正确”答案是错误的。事实证明,您可以在 AlertDialog 中将最大行数设置为 2 以上。下面是一个示例:

AlertDialog closePlayerDialog;
.........
Builder builder = new AlertDialog.Builder(this);
builder.setMessage(getString(R.string.AskToClosePlayer))
       .setPositiveButton(R.string.Yes, dialogClickListener)
       .setNeutralButton(R.string.NoJustCloseApp, dialogClickListener)
       .setNegativeButton(R.string.NoContinue, dialogClickListener);
closePlayerDialog = builder.create();
closePlayerDialog.setOnShowListener(new DialogInterface.OnShowListener() {
    public void onShow(DialogInterface dialog) {
        float textSize = 12.0f;
        Button positive = closePlayerDialog.getButton(AlertDialog.BUTTON_POSITIVE);
        positive.setTextSize(TypedValue.COMPLEX_UNIT_DIP, textSize);
        positive.setMaxLines(3);
        Button neutral = closePlayerDialog.getButton(AlertDialog.BUTTON_NEUTRAL);
        neutral.setTextSize(TypedValue.COMPLEX_UNIT_DIP, textSize);
        neutral.setMaxLines(3);
        Button negative = closePlayerDialog.getButton(AlertDialog.BUTTON_NEGATIVE);
        negative.setTextSize(TypedValue.COMPLEX_UNIT_DIP, textSize);
        negative.setMaxLines(3);
    }
});
closePlayerDialog.setCancelable(false);     
closePlayerDialog.show();

基本上,您可以使用 DialogInterface.onShowListener 编辑 AlertDialog 的组件 onShow

Actually the "correct" answer here is wrong. It turns out you can set maximum lines to more than 2 in AlertDialog. Here is an example:

AlertDialog closePlayerDialog;
.........
Builder builder = new AlertDialog.Builder(this);
builder.setMessage(getString(R.string.AskToClosePlayer))
       .setPositiveButton(R.string.Yes, dialogClickListener)
       .setNeutralButton(R.string.NoJustCloseApp, dialogClickListener)
       .setNegativeButton(R.string.NoContinue, dialogClickListener);
closePlayerDialog = builder.create();
closePlayerDialog.setOnShowListener(new DialogInterface.OnShowListener() {
    public void onShow(DialogInterface dialog) {
        float textSize = 12.0f;
        Button positive = closePlayerDialog.getButton(AlertDialog.BUTTON_POSITIVE);
        positive.setTextSize(TypedValue.COMPLEX_UNIT_DIP, textSize);
        positive.setMaxLines(3);
        Button neutral = closePlayerDialog.getButton(AlertDialog.BUTTON_NEUTRAL);
        neutral.setTextSize(TypedValue.COMPLEX_UNIT_DIP, textSize);
        neutral.setMaxLines(3);
        Button negative = closePlayerDialog.getButton(AlertDialog.BUTTON_NEGATIVE);
        negative.setTextSize(TypedValue.COMPLEX_UNIT_DIP, textSize);
        negative.setMaxLines(3);
    }
});
closePlayerDialog.setCancelable(false);     
closePlayerDialog.show();

Basically you edit the AlertDialog's components onShow, using DialogInterface.onShowListener.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文