android - 为警报对话框项设置自定义字体的字体

我正在以这种方式创建一个警告对话框:

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

我知道它用我的项目创建了一个 ListView ,我想为它们设置一个带有自定义字体的字体,并为每个列表项目设置最大字符数,我该怎么做?

最佳答案

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

像这样(在 style.xml 文件中):

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

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

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

R.style.CustomFontTheme 是您创建的字体的 ID。


更新:

好的,要添加您的自定义字体,您可以遵循以下要点: https://gist.github.com/artem-zinnatullin/7749076

关键是在应用程序启动时覆盖给定的字体,如下所示:

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

要点具有该方法的实现。

所以主题样式现在应该是这样的:

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

https://stackoverflow.com/questions/28303811/

相关文章:

python - 如何在 Django 中用阿拉伯字符创建 slug?

jsf - 在 Facelets 中有条件地添加 HTML 元素属性

java - try-with-resource资源创建的执行顺序

Python3 - 字符串的最后一个字符

apache-storm - Storm 用户界面不工作

c# - 生成员工卡

scala - Scala 中 N 的三和

xml - 使用 XSD 验证 XML 中的自定义日期和时间

jakarta-ee - 如何进行 payara netbeans 集成

html - 如何向 erb 中的字符串添加空格?