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

时间:2017-06-01 14:02:21

标签: 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
在第二次尝试时,我从构建器创建一个alertdialog并给出一个标题。结果是正确的标题,但标题再次出现在选择区域中。

        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

谢谢!

1 个答案:

答案 0 :(得分:1)

要仅显示一个标题,您必须拨打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();