How to set TextStyle of PagerTabStrip to Bold?

前端 未结 3 1360
醉梦人生
醉梦人生 2021-02-07 07:08

I\'m using PagerTabStrip in ViewPager to show the title of each page. Here is my XML code:



        
3条回答
  •  深忆病人
    2021-02-07 07:51

    If you'd like to style the center tab title differently from the others, then you'll need to use reflection:

    try {
        final Class pagerTitleStripClass = PagerTitleStrip.class;
        final Field mCurrTextField = pagerTitleStripClass.getDeclaredField("mCurrText");
        mCurrTextField.setAccessible(true);
    
        // mTitle is my class's PagerTabStrip member variable
        final TextView mCurrText = (TextView) mCurrTextField.get(mTitle);
        mCurrText.setTypeface(Typeface.DEFAULT_BOLD);
    } catch (final Exception e) {
        Log.w(TAG, "Exception when styling currently selected title!", e);
    }
    

    Unfortunately, this solution is subject to potentially not working anymore when updates for the Android Support Library are released (as that's where the PagerTitleStrip class is from). In particular, this code sample is working with revision 20.

提交回复
热议问题