Images for AlertDialog buttons

后端 未结 6 2014
再見小時候
再見小時候 2021-01-11 14:19

Is it possible to add drawables to the positive, negative and neutral buttons of an AlertDialog? If yes, then how?

6条回答
  •  猫巷女王i
    2021-01-11 15:03

    After you have built the AlertDialog in onCreateDialog you can use the following code in onPrepareDialog to add an image to the positive button:

    @Override
    protected void onPrepareDialog(int id, Dialog dialog) {
        super.onPrepareDialog(id, dialog);
        AlertDialog alertDialog = (AlertDialog)dialog;
        Button button = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
        button.setCompoundDrawablesWithIntrinsicBounds(this.getResources().getDrawable(
                R.drawable.icon), null, null, null);
    
    }
    

    Trying to add the drawable to the button in the onCreateDialog method does not seem to work.

提交回复
热议问题