我正在为我的Android应用程序定义样式XML。我有一些我想要使用的TTF文件,如何设置字体使用这些文件作为字体而不是通用的“sans”,“serif”& “等宽”。感谢
答案 0 :(得分:45)
您只能通过Java代码使用自定义字体,而不能通过布局XML或样式/主题使用 - 抱歉!
答案 1 :(得分:16)
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 文件夹中。
答案 2 :(得分:7)
<?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>
答案 3 :(得分:5)
答案 4 :(得分:0)
在资源文件夹上创建一个字体目录,然后粘贴您的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级别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);