java - 为什么 Android 对话框标题不显示在 Android 版本的棉花糖上?

标签 java android kotlin dialog customdialog

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

        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 版本的 lollipop 上运行了它,它工作正常这里是 lollipop 上的屏幕截图:

Dialog on Android version Lollipop

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

Dialog on android version marshmallow

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

myDialog.create()

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

最佳答案

您是否尝试过应用自定义样式...对话框采用可能已设置为 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))

关于java - 为什么 Android 对话框标题不显示在 Android 版本的棉花糖上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54323115/

相关文章:

java - 如何在 Struts 2 的一个 Action 类中使用不同的验证方法

java - Spring CrudRepository保存方法的自定义返回类型(动态投影)

android - MPAndroidChart PieChart 如何设置标签文字?

kotlin - RxKotlin-订阅TestSubscriber时不发出Single.just()

java - 如何在 R 脚本中运行可执行 .jar 文件?

android - Xamarin MediaPicker CapturePhotoAsync 在按下 "Ok"按钮后重新启动应用程序并抛出无提示错误

java - 使用线程和 USB 连接后应用程序屏幕空白

android - Flutter:在应用程序关闭或在后台时运行 native Android 代码

exception - Kotlin 协程无法处理异常

java - Windows XP 是否支持 SWT Java 程序中的 unicode 按钮文本?