【问题标题】:How can I use Roboto font for All views in my android app? [duplicate]如何在我的 android 应用程序中为所有视图使用 Roboto 字体? [复制]
【发布时间】:2014-07-21 19:47:58
【问题描述】:

我想在我的 android 应用程序中为所有视图使用 Roboto 字体,我该怎么做?

【问题讨论】:

  • 在下面的答案中,使用 Typeface createfromasset 时要注意内存泄漏。搜索那个问题。你的问题之前也有报道过。

标签: android


【解决方案1】:

创建自定义视图,例如 TextView、EditText 等。然后在构造函数中设置下面的 setTypeface 示例:

public class RobotoTextView extends TextView {

    public RobotoTextView(Context context) {
        super(context);
        setTypeface(Typeface
                .createFromAsset(context.getAssets(), "fontname.ttf"));

    }
}

然后在你的布局中使用它,而不是使用标准的 TextView 使用下面的自定义类示例:

<packagename.RobotoTextView
            style="@style/usecustomstyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/yourid"/>

【讨论】:

  • 有没有办法直接从xml使用
  • 参见下面的 xml 示例,您可以在任何 xml 布局中使用它。就像您使用普通的 Android 项目一样。只需用你的包名和类名访问它
  • 我可以使用android:fontFamily="Roboto"
  • 是的,只需将字体放在您的资产文件夹中并使用它。所以你可以使用任何字体!
【解决方案2】:

使用字体。将您的字体文件放在 assest 文件夹中。然后写下面的代码。

 Typeface Roboto = Typeface.createFromAsset(getAssets(), "Roboto.ttf"); 

然后使用 Roboto 字体你所有的TextView,EditText..etc

  Textview.setTypeface(italic);  

【讨论】:

    猜你喜欢
    • 2012-02-14
    • 2012-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-22
    相关资源
    最近更新 更多