Dialog.setTitle无法显示标题

16

我试图为我的对话框添加自定义标题,但无论何时运行应用程序,它都不会显示标题。

我创建对话框的代码如下:

 final Dialog passwordDialog = new Dialog(this);
 passwordDialog.setContentView(R.layout.admin_password_dialog);
 passwordDialog.setTitle("Enter An Administrative Password");
 passwordDialog.show();

我的布局文件是

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

<Button
    android:id="@+id/btn_confirmPassword"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentStart="true"
    android:layout_below="@+id/edit_adminPassword"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:text="@string/confirmPassword"/>

<EditText
    android:id="@+id/edit_adminPassword"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:ems="10"
    android:inputType="textPassword"/>

这是我得到的内容:

这是我得到的内容

我是否漏掉了什么?


这个链接可能会给你一个解决办法:https://dev59.com/ClPTa4cB1Zd3GeqPnOPU - Gavriel
4个回答

31

你应该像这样定义你的样式:

 <style name="Dialog" parent="Theme.AppCompat.Dialog">
    <item name="android:windowNoTitle">false</item>
    <item name="android:windowIsFloating">true</item>
</style>

然后将此样式传递给对话框的构造函数

final Dialog passwordDialog = new Dialog(this,R.style.Dialog);

4
这被称为解决方案,建议使用 AlertDialogue 而不是 dialogue 并不是解决方案,这可能只是一种权宜之计... - kAmol
6
为什么安卓开发让我想撞桌子? - Daniel Wilson
@Raafat Alhoumaidy 在应用此代码后,对话框的背景变成了黑色,标题颜色为白色,为什么会这样? - KJEjava48

9

像其他答案一样,但更加简洁

final AlertDialog diag = new AlertDialog.Builder(this)
        .setTitle("Enter An Administrative Password")
        .setView(R.layout.admin_password_dialog)
        .create();

diag.show();

Button diagButton = (Button) diag.findViewById(R.id.btn_confirmPassword);
diagButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        // handle button click
        EditText input = (EditText) diag.findViewById(R.id.edit_adminPassword);
        String s = input.getText().toString();
    }
});

2
@RyanNewman - 你可以使用 android.support.v7.app.AlertDialog。如果你看一下我的答案,它也更加全面,比如如何从对话框中获取按钮和编辑文本。 - OneCricketeer
1
@RyanNewman - 显然,你必须先调用show(),我已经进行了编辑...这并不是很直观... :/ - OneCricketeer
就像另一个人说的那样,这个方法从 API 1 开始就应该被支持了。我认为 Android Studio 可能有些混淆了。通常情况下,您可以安全地忽略这些警告,直到它们真正成为一个大问题。 - OneCricketeer
1
如果你使用的是import android.app.AlertDialog;,而不是我提到的支持库版本,我会认为警告就存在那里。 - OneCricketeer
当我在4.4.4模拟器上运行它时,它会崩溃,我不确定原因是什么。 - Ryan Newman
显示剩余5条评论

5
您也可以尝试此方法,并根据所使用的主题获得不同的视图样式。
<style name="FilterDialogTheme" parent="@android:style/Theme.Holo.Light.Dialog">
    <item name="android:windowNoTitle">false</item>
</style>

在对话框构造函数中
  public FilterDialog(Context context) {
        super(context, R.style.FilterDialogTheme);
    }

使用@style/Theme.Appcompat.Light.Dialog主题来进行项目开发。


2

您应该使用 AlertDialog.Builder 来创建对话框,而不是直接创建一个 Dialog

// 1. Instantiate an AlertDialog.Builder with its constructor
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

// 2. Chain together various setter methods to set the dialog characteristics
builder.setView(R.layout.admin_password_dialog);
builder.setTitle("Enter An Administrative Password");

// 3. Get the AlertDialog from create()
AlertDialog dialog = builder.create();
dialog.show();

请参考此处的Android开发者指南,了解有关对话框的信息。


这对我的设备完美运行,但我收到了一个警告,这不适用于低于棒棒糖的手机。我的当前最低API级别为16,这会在低于棒棒糖的设备上工作吗? - Ryan Newman
我在一个4.4.4模拟器上运行它,结果出现了java.lang.NoSuchMethodError: android.app.AlertDialog$Builder.setView的错误。 - Ryan Newman
我解决了。我将布局添加到默认构造函数 new AlertDialog.Builder(this, R.layout.admin_password_dialog) 中,现在它可以工作了。谢谢你的帮助。 - Ryan Newman
@RyanNewman - 第一次看错文档了。应该是一个 R.style 主题资源作为第二个参数 - OneCricketeer
我撤回之前的说法。当我将布局添加到构造函数中时,它会占用整个屏幕而没有我的自定义布局。 - Ryan Newman
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接