java - 将主题应用于 AlertDialog Builder 会导致标题无法正常工作

标签 java android

我正在尝试向我的 AlertDialog Builder 添加标题。当我添加主题时,我的标题会移动到选择区域中。
这是第一个示例:

    classificationButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            AlertDialog.Builder builder = new AlertDialog.Builder(
                    new ContextThemeWrapper(mContext, android.R.style.Theme_Holo_Dialog) );
                    //building my selection options
                    builder.setItems(classificationList, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            String desiredClassification = classificationList[which];
                            if ( !getClassification().equals(desiredClassification) ) {
                                CallsignContract.updateClassification(desiredClassification, mContext);
                                setClassification(desiredClassification);
                                classificationButton.setText(desiredClassification);
                            }
                        }
                    });
                    builder.setTitle(R.string.classification_alert_header)
                            .create().show();
        }
    });

这是结果。
no title
在第二次尝试中,我从构建器创建了一个警报对话框并为其命名。结果是正确的标题,但标题再次出现在选择区域。

        classificationButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            AlertDialog.Builder builder = new AlertDialog.Builder(
                    new ContextThemeWrapper(mContext, android.R.style.Theme_Holo_Dialog) );
                        //building my selection options
                        builder.setItems(classificationList, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            String desiredClassification = classificationList[which];
                            if ( !getClassification().equals(desiredClassification) ) {
                                CallsignContract.updateClassification(desiredClassification, mContext);
                                setClassification(desiredClassification);
                                classificationButton.setText(desiredClassification);
                            }
                        }
                    });
            AlertDialog alertDialog = builder.create();
            alertDialog.setTitle(R.string.classification_alert_header);
            alertDialog.show();
        }
    });


double titles

谢谢!

最佳答案

为了只显示一个标题,您必须调用 alertDialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE)

AlertDialog.Builder builder = new AlertDialog.Builder(
    new ContextThemeWrapper(mContext, android.R.style.Theme_Holo_Dialog)
);

//building my selection options
builder.setItems(classificationList,
    new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            String desiredClassification = classificationList[which];

            if (!getClassification().equals(desiredClassification)) {
                CallsignContract.updateClassification(desiredClassification, mContext);
                setClassification(desiredClassification);
                classificationButton.setText(desiredClassification);
            }
        }
    }
);

AlertDialog alertDialog = builder.create();
alertDialog.setTitle(R.string.classification_alert_header);
// Requesting dialog to remove the title
alertDialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
alertDialog.show();

关于java - 将主题应用于 AlertDialog Builder 会导致标题无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44309680/

相关文章:

android - 通话记录返回所有通话记录和短信记录,我怎样才能只过滤通话记录?

android - Lollipop 中的 WebView 不会从资源中加载字体

java - MVC-听起来是 View 还是模型?

java - 用于获取多行字符串中 2 个标签之间的内容的正则表达式

java - JPanel 与 GroupManager

java - 是否可以在原生 Android 应用程序中使用 WebSockets 或类似的?

javascript - 使用 javascript 或 jquery 在 webview 中显示本地 android 图像

java - 如何在我创建的目录中创建文本文件(Android外部存储)

java - swing:在 JDialog 上设置光标

java - 尽管在主要 Activity 中工作,但 native 函数在自定义 View 中抛出 UnsatisfiedLinkError