2017-03-17 38 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