如何将PagerTabStrip的TextStyle设置为粗体?

17

我在ViewPager中使用PagerTabStrip来显示每个页面的标题。这是我的XML代码:

<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属性。我该如何将样式设置为粗体呢?


@style/TitleStripTextAppearance 中有什么内容? - CommonsWare
@CommonsWare 哦,我在发布问题时忘记删除这行了。我试图在@style/TitleStripTextAppearance中添加样式,但我不确定这是否有效? - user1310152
3个回答

43

你需要做的是为文本创建样式,然后使用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"/>

12
顺便提一句,Eclipse在android.support.v4.view.PagerTabStrip中不会自动建议android:textAppearance。但不要被这个困惑,textAppearance对于PagerTabStrip仍然有效。 - Eugene Chumak
1
我需要为我的PagerTabStrip使用自定义字体。有什么办法可以实现吗? - ambit
@Justin - textMultiLine 属性不是以这种方式工作的。有其他方法吗? - Braj
1
有没有想法如何为中心选项卡标题设置特定样式?例如,用户当前选项卡的标题应该是粗体,其他选项卡则是普通字体? - Charles Madere
1
Android Studio没有textAppearance。 - therealprashant

4

PagerTabStrip不接受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类来自此处)。特别是,这段代码示例适用于修订版20


我个人不建议使用反射来实现这种“技巧”。它是私有的,有其原因 - 它可能随时更改 - 即使在同一版本的不同提交中也可能如此。反射应该用于元级别的东西,比如编写框架等。 - kamathln

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接