2017-03-17 86 views
0

我有我自己的字体,我想用于我的应用程序的所有布局,我想更改我的应用程序的默认字体Android Studio自定义字体为stadard /默认字体?

在styles.xml我可以使用

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

改变更改字体,但我怎么在这里用我的自定义字体?

应用程序 - > SRC - >主,我做了所谓的资产一个新的目录,然后又目录中的一个叫字体,而这正是我把我的自定义字体。

这可能吗?如果是这样,怎么样?

EDIT

正如mayosk说,我添加

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

并取得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() 
    ); 
} 
} 

但字体仍然是相同的,我错过了什么

+0

这可能有助于[http://stackoverflow.com/a/29134056/7676637] – AwaisMajeed

+0

可能重复的:http://stackoverflow.com/questions/2711858/is-它-可能-TO-设置一个自定义字体为整个应用程序 –

回答

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)); 
} 
所有
+0

我编辑我的帖子,我试过这个,但没有设法解决它。我做错什么了吗? –

+0

我编辑了我的答案,您需要重写您的活动类中使用该字体的attachBaseContext方法...如果您想在所有活动中完成此操作,请创建一个BaseActivity类,在此重写此方法并继续其他活动 – mayosk

+0

好的它现在有效,非常感谢! –

0

首先,你需要创建延伸的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上也更高版本。通过这样做,所有的textviews和edittext都有相同的字体整个应用程序。

+0

我可以看到这将如何解决我的问题,但那么我将不得不为我使用的所有不同视图创建自定义视图,对吗?但是,如果是这种情况,mayosk的解决方案不起作用,我会尝试这个并看看。 –

0

我有一种特殊的方式来改变默认的字体。我设整油漆创建自定义视图和的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