[android]尝试使用在 setTypefaceViewPager

发布时间: 2016/9/2 21:54:32
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.
 public class HelpSettingsFragment extends Fragment {

     * The pager widget, which handles animation and allows swiping horizontally to access previous
     * and next wizard steps.
    private ViewPager mPager;

     * The pager adapter, which provides the pages to the view pager widget.
    private PagerAdapter mPagerAdapter;

    Typeface champagneRegularFont;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.tutorial, container, false);

        ActionBar actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
        SpannableString s = new SpannableString("Help");
        s.setSpan(new TypefaceSpan(getContext(), "Candy.ttf"), 0, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        champagneRegularFont = Typeface.createFromAsset(getActivity().getAssets(),"fonts/Champagne & Limousines.ttf");

        // Instantiate a ViewPager and a PagerAdapter.
        mPager = (ViewPager) rootView.findViewById(R.id.vpPager);
        mPagerAdapter = new CustomPagerAdapter(getContext());

        return rootView;

     * A simple pager adapter that represents 5 ScreenSlidePageFragment objects, in
     * sequence.
    public class CustomPagerAdapter extends PagerAdapter {

        private Context mContext;

        public CustomPagerAdapter(Context context) {
            mContext = context;

        public Object instantiateItem(ViewGroup collection, int position) {
            ModelObject modelObject = ModelObject.values()[position];
            LayoutInflater inflater = LayoutInflater.from(mContext);
            ViewGroup layout = (ViewGroup) inflater.inflate(modelObject.getLayoutResId(), collection, false);

            return layout;

        public void destroyItem(ViewGroup collection, int position, Object view) {
            collection.removeView((View) view);

        public int getCount() {
            return ModelObject.values().length;

        public boolean isViewFromObject(View view, Object object) {
            return view == object;

        public CharSequence getPageTitle(int position) {
            ModelObject customPagerEnum = ModelObject.values()[position];

            return mContext.getString(customPagerEnum.getTitleResId());


    public enum ModelObject {

        FIRST(R.string.blue, R.layout.tutorial_1),
        SECOND(R.string.red, R.layout.tutorial_2),
        THIRD(R.string.green, R.layout.tutorial_3);

        private int mTitleResId;
        private int mLayoutResId;

        ModelObject(int titleResId, int layoutResId) {
            mTitleResId = titleResId;
            mLayoutResId = layoutResId;

        public int getTitleResId() {
            return mTitleResId;

        public int getLayoutResId() {
            return mLayoutResId;



我使用的 ViewPager 所以用户可以滚动 3 不同的布局。我想设置一个自定义 typeface 一些 TextView s 和 Button s。有谁知道,实例化对象能够设置自定义 typeface 通过 setTypeface(champagneRegularFont) 吗?

编辑︰而且,将 onClickListeners 在同一个地方吗?

不再需要知道如何实现 onClickListeners ,我已经换上了 onClick 法在 xml 文件,并声明该方法在我 activity 类。

解决方法 1:

你充气 layout 一样,

ViewGroup layout = (ViewGroup)   
inflater.inflate(modelObject.getLayoutResId(), collection, false);

现在设置 typeFace 你上你的onCreateView()的初始化如下,

TextView textView=(TextView)layout.findViewById(R.id.textView);