【问题标题】:Dialog.setTitle not showing a titleDialog.setTitle 不显示标题
【发布时间】:2016-02-09 22:24:14
【问题描述】:

我正在尝试向我的对话框添加自定义标题,但是每当我运行我的应用程序时,它都不会显示标题。

我创建对话框的代码是

 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"/>

这就是我得到的

我有什么遗漏吗?

【问题讨论】:

标签: android customdialog


【解决方案1】:

你应该这样定义你的风格:

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

然后将此样式传递给Dialog的构造函数

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

【讨论】:

  • 这叫做解决方案,建议用AlertDialogue代替对话不是解决方案,可能是变通方法...
  • 为什么Android开发让我想用头撞桌子。
  • @Raafat Alhoumaidy 应用此对话框背景变为黑色,标题颜色为白色,为什么会这样??
【解决方案2】:

和其他答案一样,但更简洁

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();
    }
});

【讨论】:

  • @RyanNewman - 你可以使用android.support.v7.app.AlertDialog。如果你看我的回答,它也更具包容性,比如如何从对话框中获取 Button 和 EditText
  • 当我尝试添加 onClickLIstener 时,我得到一个空对象引用。你知道为什么吗?
  • 好吧,如果 findViewById 找不到该 ID,它会返回 null。老实说,我没有对此进行测试,但我认为它会起作用,因为setView
  • @RyanNewman - 显然,您必须先致电show(),我进行了编辑...这不是很直观...:/
  • 如果您使用的是import android.app.AlertDialog; 而不是我提到的支持库版本,我相信这就是存在警告的地方。
【解决方案3】:

你也可以试试这个方法,根据使用的主题获得不同的视图样式。

<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

【讨论】:

    【解决方案4】:

    您应该使用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();
    

    请参阅here 了解有关对话框的 Android 开发人员指南。

    【讨论】:

    • 我不确定是否有必要为 set 方法重新分配构建器,所以我自己做出了回答,以防万一:)
    • 这非常适合我的设备,但我收到警告,这不适用于低于 Lollipop 的手机。我目前的最低 API 级别是 16,这适用于棒棒糖以下的设备吗?
    • 我不太确定您为什么会收到该警告... AlertDialog 已添加到 API 级别 1 中(根据 docs),并且使用的方法也应该从 API 级别 1 开始存在
    • 我非常肯定我在许多旧项目中都使用过它......奇怪
    • 是的,我把它扔到了 4.4.4 模拟器上,我得到了 java.lang.NoSuchMethodError: android.app.AlertDialog$Builder.setView
    猜你喜欢
    • 1970-01-01
    • 2020-12-08
    • 2011-01-03
    • 2015-11-01
    • 2010-12-28
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多