How to change theme for AlertDialog

前端 未结 15 2123
名媛妹妹
名媛妹妹 2020-11-22 09:14

I was wondering if someone could help me out. I am trying to create a custom AlertDialog. In order to do this, I added the following line of code in styles.xml



        
15条回答
  •  借酒劲吻你
    2020-11-22 09:40

    I guess it cannot be done. At least not with the Builder. I'm working with 1.6 and the Implementation in Builder.create() is:

    public AlertDialog create() {
        final AlertDialog dialog = new AlertDialog(P.mContext);
        P.apply(dialog.mAlert);
        [...]
    }
    

    which calls the "not-theme-aware" constructor of AlertDialog, which looks like this:

    protected AlertDialog(Context context) {
        this(context, com.android.internal.R.style.Theme_Dialog_Alert);
    }
    

    There is a second constructor in AlertDialog for changing themes:

    protected AlertDialog(Context context, int theme) {
        super(context, theme);
        [...]
    }
    

    that the Builder just doesn't call.

    If the Dialog is pretty generic anyway, I'd try writing a subclass of AlertDialog, calling the second constructor and use that class instead of the Builder-mechanism.

提交回复
热议问题