首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何将PagerTabStrip的TextStyle设置为粗体?

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

Stack Overflow用户
提问于 2012-11-09 20:04:50
回答 3查看 16.1K关注 0票数 17

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

代码语言:javascript
复制
<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中将textStyle设置为粗体,如下所示:android:textStyle="bold"。但这在PagerTabStrip中是不可能的。看起来它只有文本的textcolor属性。将样式设置为粗体的方法是什么?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-11-17 05:46:52

您需要做的是为文本创建一个样式,然后使用android:text外观属性...

可能是这样的:

代码语言:javascript
复制
 <style name="PagerTabStripText">
      <item name="android:textSize">14sp</item>
      <item name="android:textStyle">bold</item>
      <item name="android:textColor">#a4c639</item>
 </style>

然后在您的PagerTabStrip XML中执行以下操作:

代码语言:javascript
复制
 <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"/>
票数 43
EN

Stack Overflow用户

发布于 2014-09-11 21:57:15

PagerTabStrip不接收textAppearance。

就像@Justin上面说的,但是在xml put上:

style="@style/PagerTabStripText"

不是:

android:textAppearance="@style/PagerTabStripText"

票数 4
EN

Stack Overflow用户

发布于 2014-10-09 02:04:11

如果你想让中间的标签标题不同于其他标签,那么你需要使用反射:

代码语言:javascript
复制
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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13307758

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档