2016-07-28 24 views
0
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; 

    @Override 
    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); 
     actionBar.setTitle(s); 

     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()); 
     mPager.setAdapter(mPagerAdapter); 

     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; 
     } 

     @Override 
     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); 
      collection.addView(layout); 

      return layout; 
     } 

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

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

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

     @Override 
     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種不同的佈局。我試圖設置一個自定義字體到一些TextView s和Button s。有誰知道我在哪裏實例化對象,並能夠通過setTypeface(champagneRegularFont)設置自定義字體?試圖在ViewPager中使用setTypeface

編輯:此外,我會在同一個地方添加onClickListeners嗎?

不再需要知道如何執行onClickListeners,因爲我在xml文件中用onClick方法替換了它,並在我的活動類中聲明瞭該方法。

回答

1

您正在膨脹的佈局,

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

現在設置您在onCreateView()如下初始化的字體,

TextView textView=(TextView)layout.findViewById(R.id.textView); 
textView.setTypeface(champagneRegularFont); 
+0

我已經嘗試這樣做,它會導致應用程序墜毀。錯誤說我正試圖調用null對象引用上的方法。 –

+0

實際上,這個解決方案的工作,但是,我需要包圍'textView.setTypeface(champagneRegularFont);'與'if(textView!= null)'。 –

相關問題