Java 为什么Android对话框标题没有显示在Android版本的棉花糖上?
我试图创建一个自定义对话框,下面是我的代码:Java 为什么Android对话框标题没有显示在Android版本的棉花糖上?,java,android,kotlin,dialog,customdialog,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
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>
但标题仍然没有显示出来。我该怎么办?使用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();
您是否尝试过应用自定义样式。。。该对话框采用可能已设置为NoTitle的父活动的样式
<style name="CustomDialog" parent="@style/Theme.AppCompat.Light.Dialog">
<item name="android:windowNoTitle">false</item>
</style>
使用线性/相对布局this@AbhinavGupta我已经试过了,但标题仍然没有显示在棉花糖上。谢谢,但我不能使用alert dialog,因为setView需要api级别21,我不能在我的案例中同时使用dialog和alertDialog。@AndroidExpert请参考此链接:您也可以使用alert dialog设置自定义布局。谢谢,但我已经尝试过alertDialog,但如果我使用alertDialog,我无法取消或取消它。@AndroidExpert您可以取消它。使用setCancelable(true)&&只需查看我的代码并查看确定按钮,通过使用dialog.dismise()您就可以取消它。:)谢谢,但在我的情况下,我需要关闭按钮外侧的警报对话框。如何调用按钮外侧的dialog.Disclose()或dialog.cancel()?谢谢您的回答,但我使用的是dialog,我无法在我的情况下使用警报对话框。
<style name="CustomDialog" parent="@style/Theme.AppCompat.Light.Dialog">
<item name="android:windowNoTitle">false</item>
</style>
new AlertDialog.Builder(new ContextThemeWrapper(Context, R.style.Dialog))