如何在Android Studio的片段中实现自定义字体
问题描述:
我无法使用自定义字体在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”的“字体”子文件夹。我错过了什么?我觉得这只是一个愚蠢的错误;我对这个平台很陌生。
答
试试这个,
返回您膨胀的视图并将自定义字体设置为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;
}
哇,我是对的。这是一个愚蠢的错误。我想我的noobism显示:)谢谢! – Slug03