【问题标题】:why Android dialog title not showing on android versions marshmallow?为什么 Android 对话框标题没有显示在 android 版本的棉花糖上?
【发布时间】:2019-01-23 08:41:48
【问题描述】:

我试图创建一个自定义对话框,这是我的代码:

        val myDialog = Dialog(this)
        myDialog.setContentView(R.layout.my_dialog)
        myDialog.setTitle("My Dialog!")
        myDialog.setCancelable(true)
        myDialog.show()

这里是 my_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<TextView
        android:text="Hello world!"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:textSize="30sp"/>

</android.support.constraint.ConstraintLayout>

我已经在 android 版本的棒棒糖上运行它它工作正常这里是棒棒糖上的截图:

如您所见,对话框标题正在显示,但是当我尝试在 Android 版本的棉花糖标题上运行时,此处停止显示的是棉花糖上的屏幕截图:

如您所见,对话框标题未显示在 android 版本的棉花糖上。我尝试添加此行以显示标题:

myDialog.create()

但标题仍未显示。我该怎么办?

【问题讨论】:

  • 为此使用线性/相对布局
  • @AbhinavGupta 我已经尝试过了,但标题仍然没有显示在棉花糖上。

标签: java android kotlin dialog customdialog


【解决方案1】:

您是否尝试过应用自定义样式...对话框采用可能已设置为 NoTitle 的父 Activity 的样式。

<style name="CustomDialog" parent="@style/Theme.AppCompat.Light.Dialog">
    <item name="android:windowNoTitle">false</item>
</style>

关于java代码

new AlertDialog.Builder(new ContextThemeWrapper(Context, R.style.Dialog))

【讨论】:

  • 感谢您的回答,但我正在使用对话框,在我的情况下无法使用警报对话框。
【解决方案2】:

使用 AlertDialog 代替 Dialog。

  AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
        alertDialogBuilder.setTitle(getString(R.string.app_name));
        alertDialogBuilder.setMessage(getString(R.string.disclaimer))
                .setCancelable(false)
                .setPositiveButton("Ok",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                dialog.dismiss();
                            }
                        });
        AlertDialog alert = alertDialogBuilder.create();
        alert.show();

【讨论】:

  • 谢谢,但我无法使用警报对话框,因为 setView 需要 api 级别 21,我无法同时使用对话框和警报对话框。
  • @AndroidExpert 参考此链接:stackoverflow.com/questions/22655599/… 您还可以使用警报对话框设置自定义布局。
  • 谢谢,但我已经尝试过 alertDialog 但如果我使用警告对话框,我无法取消或关闭它。
  • @AndroidExpert 你可以关闭它。使用 setCancelable(true) && 只需查看我的代码并查看确定单击按钮,通过使用 dialog.dismiss() 您可以将其关闭。 :)
  • 谢谢,但在我的情况下,我需要关闭按钮侧的警报对话框,如何在按钮侧调用 dialog.dismiss() 或 dialog.cancel()?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-08-31
  • 1970-01-01
  • 2016-07-30
  • 1970-01-01
  • 1970-01-01
  • 2016-02-06
  • 2016-02-13
相关资源
最近更新 更多