2012-11-09 16 views
17

我在ViewPager中使用PagerTabStrip来显示每个页面的标题。这里是我的XML代码:如何将PagerTBStrip的TextStyle设置为粗体?

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/bg"> 

    <android.support.v4.view.ViewPager 
     android:id="@+id/pager" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
    > 
    <android.support.v4.view.PagerTabStrip 
      android:id="@+id/pager_title_strip" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="top" 
      android:background="#33b5e5" 
      android:textColor="#a4c639" 
      android:paddingTop="4dp" 
      android:paddingBottom="4dp" /> 
    </android.support.v4.view.ViewPager> 

</RelativeLayout> 

我想要的文字样式设置为粗体在PagerTabStrip这样的: android:textStyle="bold"。但这在PagerTabStrip中是不可能的。似乎它只有文本的textcolor属性。我可以将样式设置为大胆的方式是什么?

+0

你在'@ style/TitleStripTextAppearance'中有什么? – CommonsWare

+0

@CommonsWare哦,我忘了在发布问题时删除此行。我试图在'@ style/TitleStripTextAppearance'中添加样式,但是我不确定这是否会起作用? – Jaguar

回答

40

你需要做的是创造的文本样式,然后使用android:textAppearance属性...

也许是这样的:

<style name="PagerTabStripText"> 
     <item name="android:textSize">14sp</item> 
     <item name="android:textStyle">bold</item> 
     <item name="android:textColor">#a4c639</item> 
</style> 

,然后在PagerTabStrip XML做到这一点:

<android.support.v4.view.PagerTabStrip 
     android:id="@+id/pager_title_strip" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="top" 
     android:background="#33b5e5" 
     android:paddingTop="4dp" 
     android:paddingBottom="4dp" 
     android:textAppearance="@style/PagerTabStripText"/> 
+11

顺便说一句,日食不android android.support.v4.view.PagerTabStrip autosuggest android:textAppearance。但不要被这个混淆,textAppearance适用于PagerTabStrip。 –

+1

我需要为我的pagertabstrip使用自定义字体。任何想法如何使用? – ambit

+0

@Justin - 'textMultiLine'属性不能以这种方式工作。任何其他方式? – Braj

4

PagerTabStrip不aceppts textAppearance。

像@Justin上面说的,但对XML的说:

style="@style/PagerTabStripText"

不是:

android:textAppearance="@style/PagerTabStripText"

2

如果你想从别人不同风格的中心分页标题,那么你需要使用反射:

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

不幸的是,当Android Support Library的更新发布时(因为这是PagerTitleStrip类的来源),此解决方案可能不再适用。特别是,这个代码示例正在使用revision 20

+0

我个人不会推荐使用这些“技巧”的反射。有一个原因是私人的 - 即使在同一次修订的不同提交期间,它也可能随时发生变化。 反射应该用于像写框架等元级别的东西。 – kamathln

相关问题