簡體   English   中英

如何在Android Studio中的片段中實現自定義字體

[英]How to Implement a Custom Font in a Fragment in Android Studio

我無法在Android Studio 1.5版的Fragment中使用自定義字體。 這是我到目前為止在Java文檔中擁有的內容。

@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"
    />

字體不變,輸出保持默認類型。 我已經在主項目文件夾中創建了一個Assets文件夾,帶有一個“ fonts”子文件夾,其中包含文件“ COOPBL.TTF”。 我想念什么? 我覺得這只是一個愚蠢的錯誤; 我是該平台的新手。

嘗試這個,

返回您膨脹的視圖,並將自定義字體設置為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;
}

你可以試試這個

@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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM