设置自定义字体为Android碎片 [英] Set custom font for Android fragments
问题描述
我一直使用杰克沃顿商学院的ViewPagerIndicator 和我目前正在落实对我的片段一个自定义的字体。我已经使用这个code尝试:
I have been using Jake Wharton's ViewPagerIndicator and I am currently trying to implement a custom font on one of my fragments. I've tried using this code:
TextView txt = (TextView) findViewById(R.id.Zipcode);
Typeface font = Typeface.createFromAsset(getAssets(), "fonts/customfont.ttf");
txt.setTypeface(font);
在的onCreate
的主要活动导致空指针异常logcat中偶尔字体不能发
。我也试着设置在片段本身的字体在两个的onCreate
和 onCreateView
然而 findViewById
和 getAssests()
是未知的方法在片段范围。
In the onCreate
for the main activity which results in a null pointer exception in logcat and occasionally typeface cannot be made
. I've also tried to set the font in the fragment itself in both the onCreate
and onCreateView
however findViewById
and getAssests()
are unknown methods in the fragment scope.
我无法搞清楚如果字体的问题或者我想设置fon't的问题。
I'm having trouble figuring out if the font is the problem or where I am trying to set the fon't is the problem.
推荐答案
您可以试试这个
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_layout, container, false);
TextView txt = (TextView) v.findViewById(R.id.Zipcode);
Typeface font = Typeface.createFromAsset(getActivity().getAssets(), "fonts/customfont.ttf");
txt.setTypeface(font);
return v;
}
有了这个,你可以在片段的范围的范围内,并获得视图和资产
with this you can have the context in the scope of the fragment and get the view and the assets
这篇关于设置自定义字体为Android碎片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!