我正在使用滑动屏幕进行Android应用。我做了以下教程:http://developer.android.com/training/animation/screen-slide.html#fragment
我想使用html标签对我的字符串值中的文本进行一些编辑。我希望能够做段落和粗体字。但我不知道怎么样,我似乎无法在任何地方找到解决方案。通常我只是用这个:
textView.setText(Html.fromHtml(getString(R.string.instruct)));
但是我不明白我如何使用Fragment类和ViewPager来做到这一点。
以下是我的Fragment类:
public class InstructSSPageFrag1 extends Fragment{
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.instruct, container, false);
return rootView;
}
}
这是instructionactivity_screen_slide.xml:
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
这是我的Fragment活动以及适配器和寻呼机适配器:
public class InstructionActivity extends FragmentActivity{
private static final int NUM_PAGES=5;
private ViewPager mPager;
private PagerAdapter mPagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.instructionactivity_screen_slide);
// Instantiate a ViewPager and a PagerAdapter.
mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
}
public void onBackPressed() {
if (mPager.getCurrentItem() == 0) {
super.onBackPressed();
} else {
// Otherwise, select the previous step.
mPager.setCurrentItem(mPager.getCurrentItem() - 1);
}
}
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return new InstructSSPageFrag1();
}
@Override
public int getCount() {
return NUM_PAGES;
}
}
}
是否可以为Fragment class / Viewpager使用html标签?
编辑:我尝试使用Atul O Holic的代码
public class InstructSSPageFrag1 extends Fragment{
private TextView tvFrag;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.instruct, container, false);
String text = getActivity().getString(R.string.instructionstring);
tvFrag.setText(Html.fromHtml(text));
return rootView;
}
然而,当我进入片段时它会导致力量关闭。以下是错误:
03-20 16:14:06.329: E/AndroidRuntime(7039): FATAL EXCEPTION: main
03-20 16:14:06.329: E/AndroidRuntime(7039): java.lang.NullPointerException
03-20 16:14:06.329: E/AndroidRuntime(7039): at com.apps.swipeapp.InstructSSPageFrag1.onCreateView(InstructSSPageFrag1.java:18)
03-20 16:14:06.329: E/AndroidRuntime(7039): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
03-20 16:14:06.329: E/AndroidRuntime(7039): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
03-20 16:14:06.329: E/AndroidRuntime(7039): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
03-20 16:14:06.329: E/AndroidRuntime(7039): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
03-20 16:14:06.329: E/AndroidRuntime(7039): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
03-20 16:14:06.329: E/AndroidRuntime(7039): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
03-20 16:14:06.329: E/AndroidRuntime(7039): at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
03-20 16:14:06.329: E/AndroidRuntime(7039): at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
03-20 16:14:06.329: E/AndroidRuntime(7039): at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
03-20 16:14:06.329: E/AndroidRuntime(7039): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
03-20 16:14:06.329: E/AndroidRuntime(7039): at android.view.View.measure(View.java:15473)
03-20 16:14:06.329: E/AndroidRuntime(7039): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5056)
答案 0 :(得分:1)
首先使用Fragment中的上下文从strings.xml文件中访问字符串。
String text = getActivity().getString(R.string.yourString);
然后以您遵循的常规方式应用格式。
myFragmentTextView.setText(Html.fromHtml(text));
您也可以在一行中完成。希望这可以帮助。 :)
修改强>
初始化TextView,
tvFrag = (TextView) rootView.findViewById(R.id.yourTextViewID);
然后再做,
tvFrag.setText(Html.fromHtml(text));