无法为正按钮设置字体

时间:2015-07-27 19:19:26

标签: java android

我正在尝试将POSITIVE_BUTTONAlertDialog的字体设置为monospace,但字体不会改变。这是代码:

    // Title text
    TextView settingsTitle =  new TextView(this);
    settingsTitle.setText("Settings");
    settingsTitle.setPadding(20, 20, 20, 20);
    settingsTitle.setTextSize(20);
    settingsTitle.setTypeface(Typeface.MONOSPACE);

    AlertDialog settingsDialog = new AlertDialog.Builder(this)
            .setCustomTitle(settingsTitle)
            .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    // continue with delete
                }
            })
            .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    // do nothing
                }
            })
            .setIcon(android.R.drawable.ic_dialog_alert)
            .show();

    Typeface standardFont = Typeface.MONOSPACE;
    Button positiveButton = settingsDialog.getButton(AlertDialog.BUTTON_POSITIVE);
    positiveButton.setTypeface(standardFont);

1 个答案:

答案 0 :(得分:0)

您需要创建自定义对话框:

        final Dialog dialog = new Dialog(context);
        dialog.setContentView(R.layout.custom);
        dialog.setTitle("Title...");

        Button dialogButton = (Button)dialog.findViewById(R.id.dialogButtonOK);
        Typeface standardFont = Typeface.MONOSPACE;
        dialogButton.setTypeface(standardFont)

        // if button is clicked, close the custom dialog
        dialogButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

您可以创建对话框布局,使其看起来像您想要的任何内容。