Android:如何更改AlertDialog消息和按钮的字体系列

时间:2015-04-28 06:33:44

标签: android android-alertdialog android-fonts

我在单击按钮时显示AlertDialog,它有正负按钮,这是AlertDialog的默认按钮,带有消息。

我想更改这些按钮的字体系列&消息,我该如何更改?

以下是我的代码:

AlertDialog.Builder altDialog = new AlertDialog.Builder(context);
altDialog.setMessage(Constants.WARNING_STEPS);
altDialog.setTitle(Constants.WARNING);
altDialog.setCancelable(true);

altDialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
  @Override
    public void onClick(DialogInterface dialog, int which) {

});

altDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {

    }
});

altDialog.show();

3 个答案:

答案 0 :(得分:2)

这里的关键点是android.R.id文件中的textviews和按钮的地址,它是android.R.id.button1和android.R.id.button2和android.R.id.button3以及android .R.id.message

       AlertDialog dialog = new AlertDialog.Builder(MainActivity.this)                                                                                                         
                    .setMessage("آیا مایل به خروج از حساب کاربری هستید؟")
                    .setCancelable(true)
                    .setPositiveButton("بله", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                          ...

                        }

                    })
                    .setNegativeButton("خیر", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                           ...
                        }

                    }).show();

            TextView textView = (TextView) dialog.findViewById(android.R.id.message);
            textView.setTypeface(FontHelper.getIranSansTypeface(MainActivity.this));

            Button btn1 = (Button) dialog.findViewById(android.R.id.button1);
            btn1.setTypeface(FontHelper.getIranSansTypeface(MainActivity.this));

            Button btn2 = (Button) dialog.findViewById(android.R.id.button2);
            btn2.setTypeface(FontHelper.getIranSansTypeface(MainActivity.this));

答案 1 :(得分:0)

为了实现这一目标,您需要做两件事:

  • 通过alertbuilder创建AlertDialog。官方Android文档(http://developer.android.com/guide/topics/ui/dialogs.html)中的示例:

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setMessage(R.string.dialog_fire_missiles)
           .setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   // FIRE ZE MISSILES!
               }
           })
           .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   // User cancelled the dialog
               }
           });
    // Create the AlertDialog object and return it
    return builder.create();
    
    • 在警报对话框中设置TextView的字体,如下所示:

      AlertDialog dialog = builder.create();
      TextView textView = (TextView)  dialog.findViewById(android.R.id.message);
      Typeface face=Typeface.createFromAsset(getAssets(),"fonts/coolFont"); 
      textView.setTypeface(face); 
      dialog.show();
      

答案 2 :(得分:0)

AlertDialog dialog = new AlertDialog.Builder(this).setMessage("Hello world").show();
TextView textView = (TextView) dialog.findViewById(android.R.id.message);
Typeface face=Typeface.createFromAsset(getAssets(),"fonts/yourFONT"); 
textView.setTypeface(face);