它调用json文件并通过RecyclerView显示数据。
我想在片段中使用自定义字体。
这是XML的结构。
MainActivity.xml ...
<android.support.v4.view.ViewPager
android:id="@+id/vp_horizontal_ntb"
...
fragment_tab_2.xml
<android.support.v7.widget.RecyclerView
android:id="@+id/main_recycler"
...
tools:listitem="@layout/service_list"/>
service_list.xml
<TextView
android:id="@+id/movie_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="This is a title"/>
我执行以下步骤:
我把字体放在src / main / assets / fonts文件夹中。
片段java:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View x = inflater.inflate(R.layout.fragment_tab_2,null);
TextView textView = (TextView) x.findViewById(R.id.movie_title);
Typeface face = Typeface.createFromAsset(textView.getContext().getAssets(), "fonts/custom_sans.ttf");
textView.setTypeface(face);
应用程序将被编译但在我在设备上运行时崩溃了。
我的错误是什么?
登录
04-05 06:11:11.237 30326-30326/? I/art: Late-enabling -Xcheck:jni
04-05 06:11:11.298 30326-30339/com.test.teb E/HAL: load: id=gralloc != hmi->id=gralloc
04-05 06:11:11.315 30326-30326/com.test.teb W/System: ClassLoader referenced unknown path: /data/app/com.test.teb-2/lib/arm64
04-05 06:11:11.336 30326-30326/com.test.teb I/HwCust: Constructor found for class android.app.HwCustHwWallpaperManagerImpl
04-05 06:11:11.347 30326-30326/com.test.teb W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
04-05 06:11:11.509 30326-30326/com.test.teb I/HwSecImmHelper: mSecurityInputMethodService is null
04-05 06:11:11.892 30326-30326/com.test.teb I/Process: Sending signal. PID: 30326 SIG: 9
答案 0 :(得分:0)
确保片段布局(movie_title)
中存在TextView (fragment_tab_2.xml)
。
在Typeface.createFromAsset()
中,使用getActivity().getAssets()
代替textView.getContext().getAssets()
Typeface face = Typeface.createFromAsset(getActivity().getAssets(), "fonts/custom_sans.ttf");
textView.setTypeface(face);
使用inflater.inflate(R.layout.fragment_tab_2, container, null)
代替inflater.inflate(R.layout.fragment_tab_2, null)
。
最后,从片段onCreateView()
开始,必须返回View x
。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View x = inflater.inflate(R.layout.fragment_tab_2, container, null);
TextView textView = (TextView) x.findViewById(R.id.movie_title);
Typeface face = Typeface.createFromAsset(getActivity().getAssets(), "fonts/custom_sans.ttf");
textView.setTypeface(face);
..........
...............
return x;
}
希望这会有所帮助〜