2016-04-28 66 views
0

我無法使用自定義字體在Android Studio 1.5版的Fragment中工作。這是我到目前爲止在java文檔中的。如何在Android Studio的片段中實現自定義字體

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
{ 
    View view = inflater.inflate(R.layout.fragment_sponsors, container, false); 
    Typeface myTypeface = Typeface.createFromAsset(getActivity().getAssets(), "fonts/COOPPBL.TTF"); 
    TextView tv = (TextView) view.findViewById(R.id.textview1); 
    tv.setTypeface(myTypeface); 

    return inflater.inflate(R.layout.fragment_sponsors, container, false); 
} 

這是我在相關的XML代碼:

<TextView 
    android:id="@+id/textview1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/sponsors" 
    android:textSize="35sp" 
    /> 

字體不改變,輸出保持默認類型。我已經在主項目文件夾中創建了一個資產文件夾,其中包含文件「COOPBL.TTF」的「字體」子文件夾。我錯過了什麼?我覺得這只是一個愚蠢的錯誤;我對這個平臺很陌生。

回答

1

試試這個,

返回您膨脹的視圖並將自定義字體設置爲textview。

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
{ 
    View view = inflater.inflate(R.layout.fragment_sponsors, container, false); 
    Typeface myTypeface = Typeface.createFromAsset(getActivity().getAssets(), "fonts/COOPPBL.TTF"); 
    TextView tv = (TextView) view.findViewById(R.id.textview1); 
    tv.setTypeface(myTypeface); 

    return view; 
} 
+0

哇,我是對的。這是一個愚蠢的錯誤。我想我的noobism顯示:)謝謝! – Slug03

0

你可以試試這個

@Override 
public View onCreateView(
    LayoutInflater inflater, 
    ViewGroup container, 
    Bundle savedInstanceState) { 
     View v = inflater.inflate(R.layout.fragment_sponsors, container, false); 

     TextView tv = (TextView) v.findViewById(R.id.textview1); 

     Typeface font = Typeface.createFromAsset(getActivity().getAssets(), "fonts/COOPPBL.TTF""); 

     tv .setTypeface(font); 
     return v; 
}