【问题标题】:Android studio custom font as stadard/default font?Android Studio自定义字体作为标准/默认字体?
【发布时间】:2017-08-08 17:43:03
【问题描述】:

我有自己的字体,我想在所有布局中用于我的应用程序,我想更改我的应用程序的默认字体

我可以在styles.xml中使用

 <item name="android:fontFamily"></item>

要更改字体,但我如何在此处使用我的自定义字体?

App -> src -> main中,我创建了一个名为Assets的新目录,然后在该目录中创建了另一个名为Fonts的目录,这就是我放置自定义字体的地方。

这可能吗?如果有,怎么做?

编辑

正如mayosk所说,我补充了

compile 'uk.co.chrisjenx:calligraphy:2.2.0'

并制作了一个扩展 Application 的 Java 类,如下所示

public class CustomResources extends Application {

@Override
public void onCreate() {
    super.onCreate();

    CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
            .setDefaultFontPath("fonts/din_light.ttf")
            .setFontAttrId(R.attr.fontPath)
            .build()
    );
}
}

但是字体还是一样,是不是漏了什么

【问题讨论】:

标签: android


【解决方案1】:

使用书法: https://github.com/chrisjenx/Calligraphy

将依赖项添加到您的应用 build.gradle:

compile 'uk.co.chrisjenx:calligraphy:2.2.0'

并扩展您需要将此代码添加到 onCreate 方法的应用程序类

CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
                        .setDefaultFontPath("Fonts/customFont.ttf")
                        .setFontAttrId(R.attr.fontPath)
                        .build()
        );

您还需要在您的活动中覆盖 attachBaseContext 方法:

@Override
protected void attachBaseContext(Context newBase) {
    super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}

【讨论】:

  • 我编辑了我的帖子,我尝试了这个但没有设法修复它。我是不是做错了什么?
  • 我编辑了我的答案,您需要在使用该字体的活动类中覆盖 attachBaseContext 方法...如果您想在所有活动中这样做,请创建一个 BaseActivity 类来覆盖此方法然后扩展其他活动
【解决方案2】:

我有一个特殊的方法来改变默认字体。我设置了一个完整的paint来创建自定义视图并在onDraw方法上绘制字体,而不是xml方式。 像这样:

`private void initPaint(Context context)
{
    mTextPaint = new TextPaint();
    mTextPaint.setTextSize(66.46F);
    mTextPaint.setColor(Color.BLACK);

    Typeface typeface = Typeface.createFromAsset(context.getAssets(), "fonts/MFKeKe_Noncommercial-Regular.ttf");
    mTextPaint.setTypeface(typeface);
    mTextPaint.setTextSkewX(-0.5F);

}

@Override
protected void onDraw(Canvas canvas)
{
    super.onDraw(canvas);
    mStaticLayout = new StaticLayout(TEXT1,mTextPaint,canvas.getWidth(), Layout.Alignment.ALIGN_NORMAL,1.0F,0.0F,false);
    mStaticLayout.draw(canvas);
    canvas.restore();
}`

但在通常情况下, 对于仍有疑问的人,真正好的答案就在这里:

https://stackoverflow.com/a/16883281/1154026

【讨论】:

    【解决方案3】:

    首先你需要创建一个扩展Textview的基类,这里是基类

    public class MyTextView extends TextView {
    public MyTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setTypeFace(context);
    }
    
    public MyTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setTypeFace(context);
    }
    
    public MyTextView(Context context) {
        super(context);
        setTypeFace(context);
    }
    
    private void setTypeFace(Context context) {
        setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/Lato-Bold.ttf"));
    }}
    

    稍后在 xml 中

       <com.example.MyTextView
                android:id="@+id/text_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="hi" />
    

    edittext 也一样。通过这样做,所有的文本视图和编辑文本在整个应用程序中都具有相同的字体。

    【讨论】:

    • 我可以看到这将如何解决我的问题,但是我必须为我使用的所有不同视图创建自定义视图,对吧?但是,如果是这种情况并且mayosk的解决方案不起作用,我会试试这个看看。
    猜你喜欢
    • 1970-01-01
    • 2021-11-01
    • 2016-02-27
    • 2014-08-18
    • 2018-03-24
    • 2020-04-21
    • 2014-12-29
    • 2013-02-24
    • 2021-10-11
    相关资源
    最近更新 更多