在styles.xml中设置特定字体

27 xml fonts android

我正在为我的Android应用程序定义样式XML.我有一些我想使用的TTF文件,我如何设置字体使用这些文件作为字体而不是通用的"sans","serif"和"monospace".谢谢

Com*_*are 46

您只能通过Java代码使用自定义字体,而不能通过布局XML或样式/主题 - 抱歉!

  • 现在可以使用支持库,查看https://developer.android.com/preview/features/fonts-in-xml.html (5认同)
  • 因为这是一个旧答案;你知道这在以后的版本中是否有任何改变吗,@CommonsWare?我也希望能够通过在“<styles>”文档中定义字体来更改字体。 (2认同)

lut*_*oid 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;
    }

}
Run Code Online (Sandbox Code Playgroud)

attrs.xml :(在res/values中)

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="TextViewPlus">
        <attr name="customFont" format="string"/>
    </declare-styleable>
</resources>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

你会把"saxmono.ttf"放在assets文件夹中.


小智 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>
Run Code Online (Sandbox Code Playgroud)


Mus*_*san 5

是的你可以 :)

第1步:创建一个文件夹并将其命名为'font',并将其命名为.ttf. 第一步

第2步:转到style.xml并执行以下操作: - 第二步

第3步:在任何UI对象中使用样式标记: -

第三步