android - 整个应用程序的自定义字体

标签 android fonts

我想更改整个应用程序组件(TextView、Edittext、按钮等)的字体。我发现我可以为应用程序设置样式,但在这里我无法将 Assets 文件夹中的字体放入我的自定义样式 xml 中。我必须将我的自定义 TTF 字体从 asset 文件夹放入 style xml 中的 typeface 元素。我无法将等宽字体更改为我的自定义字体。我的风格是

<resources>
<style name="heading_text">
 <item name="android:textColor">#ff000000</item>
<item name="android:textStyle">bold</item>
<item name="android:textSize">16sp</item>
    <item name="android:typeface">monospace</item>      
</style>

最佳答案

您好,请访问我的博客 http://upadhyayjiteshandroid.blogspot.in/2013/01/android-custom-fonts-part-2.html您也可以在其中下载代码。

您需要做的是使用特定所需的字体创建自定义 View ,并在任何您想要的地方使用它。

假设有一个textview用于此目的,制作CustomTextView.java代码如下

package com.jitesh.customfonts;

import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;

public class CustomTextView extends TextView {

  public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
  super(context, attrs, defStyle);
  init();
 }

  public CustomTextView(Context context, AttributeSet attrs) {
  super(context, attrs);
  init();
 }

  public CustomTextView(Context context) {
  super(context);
  init();
 }

  public void init() {

   Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
    "fonts/HandmadeTypewriter.ttf");
  setTypeface(tf, 1);

  }
}

在xml中使用如下

<com.jitesh.customfonts.CustomTextView
        android:id="@+id/custom1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="70dp"
        android:text="@string/hello_Jitesh"
        tools:context=".MainActivity" >
    </com.jitesh.customfonts.CustomTextView>

还要确保您的字体在 Assets 中可用 fonts/HandmadeTypewriter.ttf

关于android - 整个应用程序的自定义字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21822981/

相关文章:

html - 字体在本地有效,但在服务器上无效

javascript根据图像是更白还是更黑来更改字体颜色

javascript - 在 Stylus css 中循环 @font-face

ios - 在我正在创建的框架中的 XCTest 期间无法加载自定义字体

Android - 丢失 PNG 图像 OnDraw 的质量

android - 解析服务器错误的 json 响应错误代码 100

android - 如何从共享首选项 (Android) 中禁用 bool 值的 Firebase 消息传递

r - embedFonts 提示 “Unknown device: pswrite”

android - 在 Android 上检测剪贴板的更改

android - Ionic & Capacitor - Android 闪屏响应