我正在尝试创建一个自定义对话框,这是我的代码:
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 上的屏幕截图:
如您所见,对话框标题正在显示,但是当我尝试在 Android 版本上运行时,marshmallow 标题停止显示,这里是 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/