【发布时间】:2010-04-01 17:46:36
【问题描述】:
我正在为我的 android 应用程序定义样式 XML。我有一些我想使用的 TTF 文件,如何设置字体以使用这些文件作为字体,而不是通用的“sans”、“serif”和“monospace”。谢谢
【问题讨论】:
我正在为我的 android 应用程序定义样式 XML。我有一些我想使用的 TTF 文件,如何设置字体以使用这些文件作为字体,而不是通用的“sans”、“serif”和“monospace”。谢谢
【问题讨论】:
您只能通过 Java 代码使用自定义字体,而不是通过布局 XML 或样式/主题——抱歉!
【讨论】:
TextViewPlus.java:
package com.example;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.TextView;
public class TextViewPlus extends TextView {
private static final String TAG = "TextView";
public TextViewPlus(Context context) {
super(context);
}
public TextViewPlus(Context context, AttributeSet attrs) {
super(context, attrs);
setCustomFont(context, attrs);
}
public TextViewPlus(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setCustomFont(context, attrs);
}
private void setCustomFont(Context ctx, AttributeSet attrs) {
TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.TextViewPlus);
String customFont = a.getString(R.styleable.TextViewPlus_customFont);
setCustomFont(ctx, customFont);
a.recycle();
}
public boolean setCustomFont(Context ctx, String asset) {
Typeface tf = null;
try {
tf = Typeface.createFromAsset(ctx.getAssets(), asset);
} catch (Exception e) {
Log.e(TAG, "Could not get typeface: "+e.getMessage());
return false;
}
setTypeface(tf);
return true;
}
}
attrs.xml:(在 res/values 中)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="TextViewPlus">
<attr name="customFont" format="string"/>
</declare-styleable>
</resources>
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:foo="http://schemas.android.com/apk/res/com.example"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.example.TextViewPlus
android:id="@+id/textViewPlus1"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:text="@string/showingOffTheNewTypeface"
foo:customFont="saxmono.ttf">
</com.example.TextViewPlus>
</LinearLayout>
您可以将“saxmono.ttf”放在 assets 文件夹中。
【讨论】:
【讨论】:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CodeFont" parent="@android:style/TextAppearance.Medium">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#00FF00</item>
<item name="android:typeface">monospace</item>
</style>
</resources>
【讨论】:
在资源文件夹中创建一个字体目录并粘贴您的 ttf 字体文件。然后创建字体资源 XML 并粘贴以下行。
<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android">
<font
android:fontStyle="normal"
android:fontWeight="400"
android:font="@font/roboto_light" />
<font
android:fontStyle="italic"
android:fontWeight="400"
android:font="@font/roboto_light_italic" />
</font-family>
现在您可以应用如下字体。还要注意属性'textStyle'
<TextView
android:textStyle="italic"
android:fontFamily="@font/family_roboto_light"
android:textColor="@color/primaryTextColor"
android:textSize="20sp"
android:gravity="center"
android:id="@+id/textView36"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="No Internet connection" />
<TextView
android:fontFamily="@font/family_roboto_light"
android:textStyle="normal"
android:textSize="20sp"
android:gravity="center"
android:id="@+id/textView37"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="No Internet connection" />
如果您想从代码中完成,请使用以下 API,但最低 API 级别为 26
Typeface typeface = getResources().getFont(R.font. roboto_light_italic);
textView.setTypeface(typeface);
支持库 26.0 在运行 Android 4.1(API 级别 16)及更高版本的设备上提供对 XML 字体功能的支持。
Typeface typeface = ResourcesCompat.getFont(context, R.font. roboto_light_italic);
【讨论】: