繁体   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