AlertDialogs setCustomTitle无法使用android.support.v7.app.AlertDialog

时间:2016-06-02 13:59:34

标签: android alertdialog android-alertdialog android-dialog

我想自定义V7 AlertDialog的TITLE颜色。 SetCustomTitle()似乎没有使用android.support.v7.app.AlertDialog。我可以看到如下所示的空白标题

enter image description here

但预计AlertDialog低于

enter image description here

创建提醒对话

private void createDialog() {
    ContextThemeWrapper ctw = new ContextThemeWrapper( mContext, R.style.TooltipTheme);
    AlertDialog.Builder builder = new CustomAlertDialogBuilder(ctw);
    builder.setTitle(mTitle);
    builder.setMessage(mMsg);
    builder.setPositiveButton(mOkButton,null);

    AlertDialog alert11 = builder.create();
    alert11.show();
    Button positiveButton =       alert11.getButton(DialogInterface.BUTTON_POSITIVE);
        positiveButton.setTextColor(mContext.getResources().getColor(R.color.BNPP_Color_Palette_PrimaryBtn));
}

style.xml

  <style name="TooltipTheme" parent="Theme.AppCompat.Dialog.Alert"></style>

CustomAlertDialogBu​​ilder

public class CustomAlertDialogBuilder  extends AlertDialog.Builder {

private final Context mContext;
private TextView mTitle;
private ImageView mIcon;
private TextView mMessage;

public CustomAlertDialogBuilder(Context context) {
    super(context);
    mContext = context;

    View customTitle = View.inflate(mContext, R.layout.alert_dialog_title, null);
    mTitle = (TextView) customTitle.findViewById(R.id.alertTitle);
    mIcon = (ImageView) customTitle.findViewById(R.id.icon);
    setCustomTitle(customTitle);

    View customMessage = View.inflate(mContext, R.layout.alert_dialog_message, null);
    mMessage = (TextView) customMessage.findViewById(R.id.message);
    setView(customMessage);


}

@NonNull
@Override
public AlertDialog.Builder setPositiveButton(CharSequence text, DialogInterface.OnClickListener listener) {
    return super.setPositiveButton(text, listener);
}

@NonNull
@Override
public AlertDialog.Builder setPositiveButton(int textId, DialogInterface.OnClickListener listener) {
    return super.setPositiveButton(textId, listener);
}

@Override
public CustomAlertDialogBuilder setTitle(int textResId) {
    mTitle.setText(textResId);
    return this;
}
@Override
public CustomAlertDialogBuilder setTitle(CharSequence text) {
    mTitle.setText(text);
    return this;
}

@Override
public CustomAlertDialogBuilder setMessage(int textResId) {
    mMessage.setText(textResId);
    return this;
}

@Override
public CustomAlertDialogBuilder setMessage(CharSequence text) {
    mMessage.setText(text);
    return this;
}

@Override
public CustomAlertDialogBuilder setIcon(int drawableResId) {
    mIcon.setImageResource(drawableResId);
    return this;
}

@Override
public CustomAlertDialogBuilder setIcon(Drawable icon) {
    mIcon.setImageDrawable(icon);
    return this;
}

}

1 个答案:

答案 0 :(得分:1)

您可以使用

禁用标准标题
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

并通过

builder.setView(inflater.inflate(R.layout.dialog_signin, null))

拥有自定义标题的视图。