Android:如何更改AlertDialog消息和按钮的字体族?

3

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

http://blog.supenta.com/2014/07/02/how-to-style-alertdialogs-like-a-pro/ - M D
在对话框中使用自定义布局,请参考此链接:https://dev59.com/_GPVa4cB1Zd3GeqP8LFP。 - Jignesh Jain
你找到任何解决方案了吗? - Shabbir Dhangot
3个回答

2

这里的关键点在于Android.R.id文件中文本视图和按钮的地址,它们分别是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));

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) {
                   // 用户取消对话框
               }
           });
    // 创建AlertDialog对象并返回
    return builder.create();
    
    • 将Alert Dialog中TextView的Typeface设置如下:

      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上进行了测试。 - MeqDotNet
感谢@MeqDotNet提出这个重要的观点。如果在显示AlertDialog之前尝试查找消息TextView,则会得到NullPointerException。先调用.show(),然后再查找它。 - martianw

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);

1
如果您能提供一下您代码的解释,那就太好了。 - Dave
...并且附上链接和归属,因为你是从2012年的https://dev59.com/yGcs5IYBdhLWcg3wMxFF#13052057复制粘贴来的。 - Martin Marconcini

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接