设置字体为警告对话框项自定义字体 [英] Set typeface with custom font for alert dialog items

查看:351
本文介绍了设置字体为警告对话框项自定义字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创造一个警告对话框是这样的:

I'm creating an alert dialog this way:

AlertDialog.Builder alertDialog = new AlertDialog.Builder(view.getContext());
alertDialog.setCustomTitle(null);
alertDialog.setItems(getAllListNames(), new DialogInterface.OnClickListener() {
  @Override
  public void onClick(DialogInterface dialogInterface, int i) {
      //Doing something awesome...
  }
});

我知道它会创建一个列表视图与我的项目,我想设置为他们的自定义字体的字体,还为每个列表项字符的最大数量,我该怎么办呢?

I know it creates a list view with my items, I want to set a typeface with a custom font for them and also set a max number of characters for each list item, how can I do it?

推荐答案

您必须创建一个自定义主题。下面是官方指南: http://developer.android.com/guide/topics /ui/themes.html :)

You have to create a custom theme. Here is the official guide: http://developer.android.com/guide/topics/ui/themes.html :)

像这样的东西(在 style.xml 文件):

Something like this (in the style.xml file):

<style name="CustomFontTheme" parent="android:Theme.Light">
    <item name="android:textViewStyle">@style/MyTextViewStyle</item>
    <item name="android:buttonStyle">@style/MyButtonStyle</item>
</style>

<style name="MyTextViewStyle" parent="android:Widget.TextView">
    <item name="android:fontFamily">sans-serif-light</item>
</style>

<style name="MyButtonStyle" parent="android:Widget.Holo.Button">
    <item name="android:fontFamily">sans-serif-light</item>
</style>

然后当你创建对话框,你可以这样做:

Then when you create the dialog you can do this:

Context context = new ContextThemeWrapper(view.getContext(), R.style.CustomFontTheme)
AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);

其中, R.style.CustomFontTheme 是他们已经创建的ID。

Where R.style.CustomFontTheme is the id for the them you have created.

更新:

好吧,来添加自定义的字体,你可以按照这个要点是:
https://gist.github.com/artem-zinnatullin/7749076

Ok, to add your custom typeface you can follow this gist: https://gist.github.com/artem-zinnatullin/7749076

关键是要覆盖一个给定的字型应用程序启动时是这样的:

The key is to override the a given font face when the application starts like this:

TypefaceUtil.overrideFont(getApplicationContext(), "AWESOME-FONT", "fonts/Awesome-Font.ttf");

要点有该方法的实现。

the gist has the implementation for that method.

所以,主题样式看起来应该是现在这个样子:

So the theme styles should look like this now:

<style name="MyTextViewStyle" parent="android:Widget.TextView">
    <item name="android:fontFamily">awesome-font</item>
</style>

<style name="MyButtonStyle" parent="android:Widget.Holo.Button">
    <item name="android:fontFamily">awesome-font</item>
</style>

这篇关于设置字体为警告对话框项自定义字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆