所以在 Android 中有不同的方法来设置你的字体。
我需要做的是将字体与 自定义字体
结合使用,从而覆盖 整个应用程序
。
现在经过一些研究,我有 2 个选择。
- 覆盖
EditText
并设置字体,在我的布局 xml 中使用此类。 - 在我的 styles.xml 中为我的 AppTheme 添加字体。
现在我更喜欢最后一个选项,这显然是我的最佳选择,因为我在整个应用程序中使用相同的字体。
现在的问题是,在我的 Assets/font/
文件夹中,我添加了我想使用的字体,但我不能在我的样式中使用它。
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:typeface"> Here? </item>
</style>
如何将我的字体添加为字样。这可能吗?
最佳答案
试试这段代码:
public class CustomTextView extends EditText {
public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
if (!isInEditMode())
init();
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
if (!isInEditMode())
init();
}
public CustomTextView(Context context) {
super(context);
if (!isInEditMode())
init();
}
public void init() {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(), Constants.FONTPATH);
setTypeface(tf);
}
}
然后在XMLwith中使用;
<pkgname.CustomTextView android:id="@+id/txt" android:layout_width="wrap_content" android:layout_height="wrap_content" />
关于android - 整个应用程序中使用的自定义字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19361036/