简体   繁体   English

Android Studio自定义字体为标准/默认字体?

[英]Android studio custom font as stadard/default font?

I have my own font that I want to use for my app in all layouts, I want to change the default font for my app . 我有自己的字体要在所有布局中用于我的应用程序, 我想更改应用程序的默认字体

In styles.xml I can use 在styles.xml中,我可以使用

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

to change the change the font, but how do I use my custom font here? 更改更改字体,但是如何在此处使用自定义字体?

In App -> src -> main , i made a new directory called Assets , then another directory in that one called Fonts , and this is where i placed my custom font. App-> src-> main中 ,我创建了一个名为Assets新目录 ,然后在其中创建了另一个名为Fonts的目录 ,这是我放置自定义字体的位置。

Is this possible? 这可能吗? If so, how? 如果是这样,怎么办?

EDIT 编辑

As mayosk said, I added 正如Mayosk所说,我补充说

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

And made a Java Class that extends Application as shown below 并制作了一个扩展应用程序的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()
    );
}
}

But the font is still the same, did I miss something 但是字体还是一样,我错过了吗

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

Add dependency to your app build.gradle: 向您的应用程序build.gradle添加依赖项:

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

and extend your application class where you need to add this code to onCreate method 并将您的应用程序类扩展到需要将此代码添加到onCreate方法的位置

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

and also you need to override attachBaseContext method() in your activity: 并且您还需要在您的活动中覆盖attachBaseContext method():

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

First of all you need to create a base class which extends Textview , here is the baseclass 首先,您需要创建一个扩展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"));
}}

later in xml 稍后在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" />

same for edittext also. 同样对于edittext。 By doing this all the textviews and edittexts have same font entire the app. 这样,整个应用程序中所有的textviews和edittexts都具有相同的字体。

I have a special way to change the default font.I set a whole paint to create custom view and draw font on onDraw method,instead of xml way。 like this: 我有一种特殊的方法来更改默认字体。我设置了整个画图以创建自定义视图并在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();
}`

But in usual, For anyone still have question, the real, good answer is here: 但是通常,对于仍然有疑问的任何人,真正的好答案是在这里:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM