我想更改整个应用程序组件(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/