如何在Android中设置特定字体的按钮文本?

37

我希望我的按钮文本能够使用 Copperplate Gothic Light 字体,但迄今为止我还没有找到一个简单干净的代码来实现这个简单的功能。请求帮助!

PS:由于安卓系统已经自带了Arial和其他一些字体,我们需要导入我们想要使用的字体(对于我这个新手来说,很抱歉用词不当)。目前这是我能搜集到的全部信息,也是我的瓶颈所在。


是的,您需要在Android项目中创建名为“assets”的文件夹,然后像这样使用它 Typeface font = Typeface.createFromAsset(getAssets(), "copperplate_gothic_light.TTF"); 然后将其用于按钮。 - Marko Niciforovic
我不明白你所说的“使用它”的意思,我应该在那个文件夹中创建什么,并将你发布的代码放在哪里? - Garima Tiwari
将您的字体文件导入项目中(从下载位置复制您的字体文件(Copperplate Gothic Light.ttf),或者如果是Windows字体,则将其复制到资产文件夹中)。然后,在您创建代码中的按钮时,或通过finditbyid引用它时,添加以下代码行。因此,紧接着应该是Typeface font = Typeface.createFromAsset(getAssets(), "copperplate_gothic_light.TTF"); button.setTypeface(font); - Marko Niciforovic
错误:无法创建本地字体。 - Garima Tiwari
好的,这意味着字体文件可能是损坏的或由于某些原因不受支持。您可以将其放在SD卡上并从FILE中加载它,以确保它不是您资产的问题。这样做很可能会起作用。 - Marko Niciforovic
请查看这个问题,它很可能会帮助你:https://dev59.com/6Wcs5IYBdhLWcg3wiEiK - Marko Niciforovic
9个回答

139
如果您计划将相同的字体添加到多个按钮中,我建议您完全实现它作为样式并对按钮进行子类化:
public class ButtonPlus extends Button {

    public ButtonPlus(Context context) {
        super(context);
    }

    public ButtonPlus(Context context, AttributeSet attrs) {
        super(context, attrs);
        CustomFontHelper.setCustomFont(this, context, attrs);
    }

    public ButtonPlus(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        CustomFontHelper.setCustomFont(this, context, attrs);
    }
}

这是一个帮助类,可以根据 com.my.package:font 属性在 TextView(记住,Button 是 TextView 的子类)上设置字体:

public class CustomFontHelper {

    /**
     * Sets a font on a textview based on the custom com.my.package:font attribute
     * If the custom font attribute isn't found in the attributes nothing happens
     * @param textview
     * @param context
     * @param attrs
     */
    public static void setCustomFont(TextView textview, Context context, AttributeSet attrs) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomFont);
        String font = a.getString(R.styleable.CustomFont_font);
        setCustomFont(textview, font, context);
        a.recycle();
    }

    /**
     * Sets a font on a textview
     * @param textview
     * @param font
     * @param context
     */
    public static void setCustomFont(TextView textview, String font, Context context) {
        if(font == null) {
            return;
        }
        Typeface tf = FontCache.get(font, context);
        if(tf != null) {
            textview.setTypeface(tf);
        }
    }

}

这里是FontCache,可减少旧设备上的内存使用

public class FontCache {

    private static Hashtable<String, Typeface> fontCache = new Hashtable<String, Typeface>();

    public static Typeface get(String name, Context context) {
        Typeface tf = fontCache.get(name);
        if(tf == null) {
            try {
                tf = Typeface.createFromAsset(context.getAssets(), name);
            }
            catch (Exception e) {
                return null;
            }
            fontCache.put(name, tf);
        }
        return tf;
    }
}

在res/values/attrs.xml中,我们定义了自定义样式属性。
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="CustomFont">
        <attr name="font" format="string"/>
    </declare-styleable>
</resources>

最后是在布局中使用的示例:

    <com.my.package.buttons.ButtonPlus
        style="@style/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_sometext"/>

并且在res/values/style.xml中

<style name="button" parent="@android:style/Widget.Button">
    <item name="com.my.package:font">fonts/copperplate_gothic_light.TTF</item>
</style>

这可能看起来是很多工作,但一旦你有了几个想要更改字体的按钮和文本字段,你会感谢我的。

嘿,这非常周到,因为我确实需要通过无数个活动来实现它。 - Garima Tiwari
这是我在StackOverflow上找到的最有帮助的答案之一。谢谢! - gnclmorais
非常好的回复。出于好奇,为什么要将FontCache作为CustomFontHelper的单独类呢?我将它们合并,并将其应用于按钮和文本视图。 - Tom S.
1
我一时想不出除了你可能想在其他情况下也使用FontCache之外,将它们分开的好理由。 - britzl
不错的答案!但是你也可以重写setTypeface方法。 - denvercoder9
显示剩余3条评论

13

1)将所需的字体作为.ttf文件(例如CopperplateGothicLight.ttf文件)获取并放置在项目的 /assets/ 目录中。

2)使用以下代码引用该字体并将其设置到您的按钮中:

Typeface copperplateGothicLight = Typeface.createFromAsset(getAppContext().getAssets(), "CopperplateGothicLight.ttf"); 
yourButton.setTypeface(copperplateGothicLight);

你说的.ttf文件在哪里可以找到?我对这方面非常陌生,所以需要一点耐心,谢谢。 - Garima Tiwari
1
这是一个TrueType字体文件。如果您没有用于您的字体的.ttf文件,只需搜索Google或其他网站,我相信有大量免费的字体可供使用。 - npace
1
如果您不缓存Typeface(我认为该错误已在较新的Android版本中得到解决),请注意在旧设备上使用内存。https://code.google.com/p/android/issues/detail?id=9904 - britzl

3

在进行了几次研究后,我最好的选择是:

public class CustomButton extends Button {

    Typeface normalTypeface = FontCache.get("fonts/CopperplateGothicLight.ttf", getContext());
    Typeface boldTypeface = FontCache.get("fonts/CopperplateGothicBold.ttf", getContext());

    /**
     * @param context
     */
    public CustomButton(Context context) {
        super(context);
    }

    /**
     * @param context
     * @param attrs
     */
    public CustomButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    /**
     * @param context
     * @param attrs
     * @param defStyleAttr
     */
    public CustomButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

}

然后,使用来自以下链接第一个答案中的fontCache:Memory leaks with custom font for set custom font
public class FontCache {
    private static Hashtable<String, Typeface> fontCache = new Hashtable<String, Typeface>();

    public static Typeface get(String name, Context context) {
        Typeface tf = fontCache.get(name);
        if(tf == null) {
            try {
                tf = Typeface.createFromAsset(context.getAssets(), name);
            }
            catch (Exception e) {
                return null;
            }
            fontCache.put(name, tf);
        }
        return tf;
    }
}

更少的代码,更多地使用Android标准!

它不起作用?你在哪里应用自定义字体? - Prashanth Debbadwar

2

MainActivity.java

    package com.mehuljoisar.customfontdemo;

import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Menu;
import android.widget.Button;

public class MainActivity extends Activity {

    private Button button1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button1 = (Button)findViewById(R.id.button1);
        button1.setTypeface(Typeface.createFromAsset(getAssets(), "copperplate-gothic-light.ttf"));
        button1.setText("hello");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="@string/hello_world" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="24dp"
    android:text="Button" />

您需要的字体下载链接: copperplate_gothic_light

将其放置在资产文件夹中。

截图: enter image description here

希望这对您有所帮助!


嘿,我做了那个。我从互联网上下载了这个东西并将其放在我的资产文件夹中,但它似乎无法识别它。文件夹的名称必须相同吗?还是我要直接放字体名称? - Garima Tiwari
@GarimaTiwari:看一下截图,你需要将那个.ttf文件放到你的assets文件夹中,并在设置按钮字体时使用相同的文件名。 - Mehul Joisar
如果您想在多个地方应用字体,britzl 的答案更适合您的情况。通过扩展 Button 来创建自定义视图,以消除在每个活动中使用 createFromAsset 方法时出现的内存泄漏问题。 - Mehul Joisar

0
您可以按照以下方式使用自定义按钮类。将字体放置在asset/font文件夹中。
public class CustomButton extends Button{


    public CustomButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
        // TODO Auto-generated constructor stub
    }
    public CustomButton(Context context) {
        super(context);
        init();
        // TODO Auto-generated constructor stub
    }
    public CustomButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
        // TODO Auto-generated constructor stub
    }
    private void init(){
        Typeface font_type=Typeface.createFromAsset(getContext().getAssets(), "font/ProximaNova-Bold.ttf");
        setTypeface(font_type);
    }
}

现在您可以按照以下提供的xml代码使用按钮。
<model.CustomButton
            android:id="@+id/search"
            android:layout_width="@dimen/edittext_width_large"
            android:layout_height="@dimen/button_height"
            android:layout_below="@+id/cars"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="@dimen/pad_20dp"
            android:background="@drawable/button_pressed_bg"
            android:text="@string/find_car"
            android:textColor="@color/white" />

0
在 Kotlin 中,您可以创建自定义按钮并覆盖字体。
class StandardButton @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, dyfStyleAttr: Int = 0) : AppCompatButton(context, attrs, dyfStyleAttr) {
    init {
        typeface = Typeface.createFromAsset(context.assets, "fonts/CopperplateGothicBold.ttf")
    }
}

0

首先下载字体样式的TTF文件,然后将其放入项目的assets文件夹中。

您可以通过以下方式编程设置它:

Typeface font_style = Typeface.createFromAsset(getAssets(), "yourcystomfontstyle.ttf");  
yourbutton.setTypeface(font_style);

首先,你不能从XML文件中设置自定义字体样式。这是错误的。你可以定义一个自定义样式属性并子类化Button。请参考我的答案。 - britzl
@britzl 哦,我不知道那个... 谢谢您提供如此有用的信息。 - baldguy
1
如果您不缓存Typeface(我认为该错误已在较新的Android版本中解决),请注意在旧设备上使用内存。https://code.google.com/p/android/issues/detail?id=9904 - britzl

0

试一试。对于EditTextViews、TextViews等都有用!

<your.namespace.app.FontButton
     app:font="montserrat"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"/>

这怎么可能?就是这样!

public class FontButton extends Button {

public FontEditText(Context context) {
    this( context, null );
}

public FontEditText(Context context, AttributeSet attrs) {
    this( context, attrs, 0 );
    init( context, attrs );
}

public FontEditText(Context context, AttributeSet attrs, int defStyle) {
    super( context, attrs, defStyle );
    init( context, attrs );
}

public FontEditText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super( context, attrs, defStyleAttr, defStyleRes );
    init( context, attrs );
}

private void init(Context context, AttributeSet attrs) {
    TypedArray ta = context.obtainStyledAttributes( attrs, R.styleable.Fonts );

    if ( ta != null ) {
        String fontAsset = ta.getString( R.styleable.Fonts_font );
        if ( !StringUtils.isEmpty( fontAsset ) ) {
            int type = Integer.parseInt( fontAsset );

            Typeface typeFace = FontManager.getInstance( context ).getByType( type );
            ta.recycle();
            super.setTypeface( typeFace );
        }
    }
}

}

public class FontManager {

private static FontManager Instance;

private Context context;

private Typeface robotoCondensedBold;
private Typeface robotoCondensed;
private Typeface robotoLight;
private Typeface kronica;
private Typeface montserrat;
private Typeface montserratLight;
private Typeface keepCalmMedium;

private FontManager(Context context) {
    this.context = context;
    this.robotoCondensedBold = Typeface.createFromAsset( context.getAssets(), "fonts/RobotoCondensed-Bold.ttf" );
    this.robotoCondensed = Typeface.createFromAsset( context.getAssets(), "fonts/RobotoCondensed-Regular.ttf" );
    this.robotoLight = Typeface.createFromAsset( context.getAssets(), "fonts/Roboto-Light.ttf" );
    this.kronica = Typeface.createFromAsset( context.getAssets(), "fonts/kronika.ttf" );
    this.montserrat = Typeface.createFromAsset( context.getAssets(), "fonts/Montserrat-Regular.ttf" );
    this.montserratLight = Typeface.createFromAsset( context.getAssets(), "fonts/Montserrat-Light.ttf" );
    this.keepCalmMedium = Typeface.createFromAsset( context.getAssets(), "fonts/KeepCalmMedium.ttf" );
}

public synchronized static FontManager getInstance(Context context) {
    if ( Instance == null )
        Instance = new FontManager( context );

    return Instance;
}

public Typeface getByType(int type) {
    switch ( type ) {
        case 0:
            return FontManager.getInstance( context ).getRobotoCondensedBold();
        case 1:
            return FontManager.getInstance( context ).getRobotoLight();
        case 2:
            return FontManager.getInstance( context ).getKronica();
        case 3:
            return FontManager.getInstance( context ).getRobotoCondensed();
        case 4:
            return FontManager.getInstance( context ).getMontserrat();
        case 5:
            return FontManager.getInstance( context ).getMontserratLight();
        case 6:
            return FontManager.getInstance( context ).getKeepCalmMedium();
        default:
            return Typeface.DEFAULT;
    }
}

public Typeface getRobotoCondensedBold() {
    return robotoCondensedBold;
}

public Typeface getKronica() {
    return kronica;
}

public Typeface getRobotoCondensed() {
    return robotoCondensed;
}

public Typeface getRobotoLight() {
    return robotoLight;
}

public Typeface getMontserrat() {
    return montserrat;
}

public Typeface getMontserratLight() {
    return montserratLight;
}

public Typeface getKeepCalmMedium() {
    return keepCalmMedium;
}

此外,在您的res文件夹中还有一个font_attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="Fonts">
        <attr name="font" format="enum">
            <enum name="robotoCondensedBold" value="0"/>
            <enum name="robotoLight" value="1"/>
            <enum name="kronica" value="2"/>
            <enum name="robotoCondensed" value="3"/>
            <enum name="montserrat" value="4"/>
            <enum name="montserratLight" value="5"/>
            <enum name="keepCalmMedium" value="6"/>
        </attr>
    </declare-styleable>
</resources> 

请注意,您只需要修改FontManagerfont_attrs.xml即可自定义字体!

嗯。我也在这里发布了相同的答案。 :) - Shashanth

0

您可以使用以下代码。只需根据您的要求替换mTextFont()方法中的字体名称即可。

public class Button_Roboto_Regular extends Button {
public Button_Roboto_Regular(Context context) {
    super(context);
    mTextFont(context);
}

public Button_Roboto_Regular(Context context, AttributeSet attrs) {
    super(context, attrs);
    mTextFont(context);
}

public Button_Roboto_Regular(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    mTextFont(context);
}
private void mTextFont(Context context) {
    Typeface face = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Regular_0.ttf");
    this.setTypeface(face);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接