2014-01-26 20 views
0

看來我不能改變我的應用程序的字體。我有粘貼資產>字體我的字體但還是現在運氣..改變字體不會在片段上工作

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle  savedInstanceState){ 
    View v = inflater.inflate(R.layout.fragment_menu2, container, false); 
    TextView txt = (TextView) v.findViewById(R.id.kernel); 
     Typeface font = Typeface.createFromAsset(getActivity().getAssets(), "EVERAFTE.TTF"); 
     txt.setTypeface(font); 
    return v; 

XML文件

<TextView 
    android:id="@+id/kernel" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:text="Kernel" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 
+0

首先,確保資本的文件名與您在Java代碼中使用的內容相匹配,並且該文件存在於項目「assets /'目錄的根目錄中。如果這還不能解決問題,請嘗試使用其他字體,例如[this font](https://github.com/commonsguy/cw-omnibus/blob/master/Fonts/FontSampler/assets/)。字體/ HandmadeTypewriter.ttf?raw = true)[示例項目](https://github.com/commonsguy/cw-omnibus/tree/master/Fonts/FontSampler)。並非所有的字體都可以在Android中使用,無論出於何種原因 – CommonsWare

+0

謝謝你的回覆。但即使鏈接上的字體也不起作用。 – user3220085

回答

0

字樣字體= Typeface.createFromAsset(getActivity()。getAssets(),「FONTS/EVERAFTE .TTF「);

0

第一:

你不應該設置型的臉onCreateView方法,你需要做的是創建視圖後。

看到:

TextView txt; 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle  savedInstanceState){ 
     View v = inflater.inflate(R.layout.fragment_menu2, container, false); 
     txt = (TextView) v.findViewById(R.id.kernel); 
     return v; 


    @Override 
    public void onViewCreated(View view, Bundle savedInstanceState) { 
    Typeface font = Typeface.createFromAsset(getActivity().getAssets(), "FONTS/EVERAFTE.TTF"); 
     txt.setTypeface(font); 
     super.onViewCreated(view, savedInstanceState); 
    } 

,並確保字體路徑是正確的。 (區分大小寫)。


編輯:

創建一個自定義的TextView和使用,在您的佈局:

import android.content.Context; 
import android.content.SharedPreferences; 
import android.graphics.Typeface; 
import android.preference.PreferenceManager; 
import android.util.AttributeSet; 
import android.widget.TextView; 

public class MyTextView extends TextView { 

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

    public MyTextView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     init(); 
    } 

    public MyTextView(Context context) { 
     super(context); 
     init(); 
    } 

    private void init() { 
     if (!isInEditMode()) { 
      Typeface font = Typeface.createFromAsset(getContext().getAssets(), "FONTS/EVERAFTE.TTF"); 
       setTypeface(font); 
     } 
    } 

} 

,然後在XML文件:

<yourpackagename.MyTextView 
    android:id="@+id/kernel" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:text="Kernel" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 
+0

我試過了。但沒有運氣。仍然沒有工作。 :( – user3220085

+0

@ user3220085看到我已經更新了答案 – Mahfa

+0

得到的活動是未定義的類型MyTextView .. – user3220085