【问题标题】:How can I change the font of items in alertDialog? [duplicate]如何更改 alertDialog 中项目的字体? [复制]
【发布时间】:2016-02-11 11:30:46
【问题描述】:

我有 alertDialog 的物品,

这是代码:

  final CharSequence[] options = { "Back","continue"};
              AlertDialog.Builder builder = new  AlertDialog.Builder(myActivity.this);
              builder.setTitle(customTitle);

                builder.setItems(options, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int item) {
                        if (options[item].equals("Back"))
                        {

                            //do something;


                        }
                        else if (options[item].equals("continue"))
                        {
                           dialog.dismiss();
                        }

                    }
                });
                builder.show();

我的资产文件夹中有自定义字体,如何将项目的字体连接到该字体? 我看到了很多答案,但对话框中的项目没有答案,所有答案都是关于 textView 使用的标题或文本, 我需要更改我的项目的字体,有人可以帮助我吗?

【问题讨论】:

  • 我查看了相同的问题,但没有找到我的具体问题的答案,是否有人可以帮助我?

标签: java android android-alertdialog


【解决方案1】:

为此,您可以使用警报构建器来构建警报。然后,您从此警报中获取 TextView,然后设置警报的字体。

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.ttf"); 
textView.setTypeface(face);

【讨论】:

  • 我试过这种方式但应用程序崩溃了,这种方式是否也将字体更改为项目?我需要在代码中的哪个位置添加这些行?也许我没有把它们放在正确的行?
  • 您必须在 onCreate 方法中添加代码。另外,您需要在资产中添加字体文件。如果您的 api
  • 如果您不能成功,请查看此帖子。 code.tutsplus.com/tutorials/… 祝你好运。
  • 这一行是什么意思? AlertDialog 对话框 = new AlertDialog.Builder(this).setMessage("Hello world").show();我需要将 alertDialog 放在哪里?
  • 链接是关于 textViews 的,alertDialog 项目呢?我不明白你的答案...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-02-28
  • 1970-01-01
  • 2023-01-20
  • 2014-10-13
  • 2019-11-23
  • 2011-11-20
  • 2019-11-02
相关资源
最近更新 更多