【问题标题】:Android: How to change font family of AlertDialog message and buttonsAndroid:如何更改 AlertDialog 消息和按钮的字体系列
【发布时间】:2015-07-06 20:44:00
【问题描述】:

我在单击按钮时显示一个 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();

【问题讨论】:

标签: android android-alertdialog android-fonts


【解决方案1】:

这里的重点是android.R.id文件中textviews和buttons的地址,即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));

【讨论】:

    【解决方案2】:

    为了实现这一点,你需要两件事:

    • 通过 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();
        

    【讨论】:

    • 我想提一下,在显示按钮和文本视图之前,您无法获取它们的值 创建对话框不足以获取在 API23 上测试的值
    【解决方案3】:
    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);
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-28
    • 2021-05-13
    • 1970-01-01
    • 2015-12-25
    • 1970-01-01
    • 2011-03-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多