Android 在styles.xml 中设置特定字体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2562408/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:20:06  来源:igfitidea点击:

Set specific font in a styles.xml

xmlandroidfonts

提问by CommonsWare

I'm defining a style XML for my android app. I have some TTF files I want to use, how can I set the typeface to use those files as the font as opposed to the generic "sans", "serif" & "monospace". Thanks

我正在为我的 android 应用定义一个样式 XML。我有一些我想使用的 TTF 文件,如何设置字体以使用这些文件作为字体,而不是通用的“sans”、“serif”和“monospace”。谢谢

回答by CommonsWare

You can only use custom fonts via Java code, not through layout XML or styles/themes -- sorry!

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

回答by luttu android

TextViewPlus.java:

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:(in res/values)

attrs.xml:(在资源/值中)

<?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>

You would put "saxmono.ttf" in the assetsfolder.

您可以将“saxmono.ttf”放在资产文件夹中。

回答by Mustafa Hasan

Yes, you can :)

是的你可以 :)

step 1: Create a folder and name it 'font' and put your .ttf inside it. Step one

第 1 步:创建一个文件夹并将其命名为“字体”并将您的 .ttf 文件放入其中。 步骤1

step 2: Go to style.xml and do the following : - step two

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

step 3: Use the style tag anywhere in views(TextView, TabLayout,..etc.):-

第 3 步:在视图中的任何位置使用样式标记(TextView、TabLayout 等):-

Step three

第三步

回答by rajeesh

<?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>

回答by Ebin Joy

enter image description here

在此处输入图片说明

Create a font dir on resource folder and paste your ttf font file. Then create a font resource XML and paste following lines.

在资源文件夹上创建一个字体目录并粘贴您的 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>

Now you can apply font as below. Also note attribute 'textStyle'

现在您可以应用如下字体。还要注意属性“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" />

If you want done from code use following, but minimum API level 26

如果您想从代码中完成,请使用以下代码,但最低 API 级别为 26

Typeface typeface = getResources().getFont(R.font. roboto_light_italic);
textView.setTypeface(typeface);

The Support Library 26.0 provides support to the Fonts in XML feature on devices running Android 4.1 (API level 16) and higher.

支持库 26.0 为运行 Android 4.1(API 级别 16)及更高版本的设备上的 Fonts in XML 功能提供支持。

Typeface typeface = ResourcesCompat.getFont(context, R.font. roboto_light_italic);