简体   繁体   English

如何在Android Studio中的片段中实现自定义字体

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

I cannot get a custom font to work in a Fragment in Android Studio version 1.5. 我无法在Android Studio 1.5版的Fragment中使用自定义字体。 Here is what I have so far in the java document. 这是我到目前为止在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);
}

And this is what I have in the pertinent XML code: 这就是我在相关XML代码中所拥有的:

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

The font does not change and the output remains the default type. 字体不变,输出保持默认类型。 I have already created an assets folder in the main project folder, with a "fonts" sub-folder which contains the file "COOPBL.TTF". 我已经在主项目文件夹中创建了一个Assets文件夹,带有一个“ fonts”子文件夹,其中包含文件“ COOPBL.TTF”。 What am I missing? 我想念什么? I feel like it is just a silly error; 我觉得这只是一个愚蠢的错误; I am very new to the platform. 我是该平台的新手。

try this, 尝试这个,

return the view which you inflate and set the custom font to textview. 返回您膨胀的视图,并将自定义字体设置为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;
}

You can try this 你可以试试这个

@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