【问题标题】:Change font style in Action Bar Tabs using Sherlock使用 Sherlock 更改操作栏选项卡中的字体样式
【发布时间】:2012-09-21 19:47:36
【问题描述】:

我读过这个:

Android - customizing actionbar sherlock tabs

还有更多链接,我已经弄清楚什么是 StackedBackground 和什么是 Background,但我找不到如何从选项卡中删除所有大写字母并获取常规字体,或更改操作栏选项卡中的字体大小。

除此之外,我想改变我的蓝色下划线的颜色,如果我使用 9 个补丁图形,我会得到与上面链接类似的情况,我的行在文本下方,但不在标签底部下方,原来的蓝线仍然存在,所以我得到了两条线。

到目前为止,我有这个,但我已经尝试了文本大小和各种颜色,但没有:

<style name="CustomActivityTheme" parent="@android:style/Theme.Holo.Light">
    <item name="android:actionBarStyle">@style/MyActionBar</item>
    <item name="android:actionBarTabTextStyle">@style/MyActionBar</item>
</style>

<style name="MyActionBar" parent="@android:style/Widget.Holo.ActionBar">
    <item name="android:background">@drawable/grey</item>
    <item name="android:backgroundStacked">@drawable/ab_transparent_example</item>
    <item name="android:textAllCaps">false</item>
    <item name="android:textSize">10dp</item>
</style>

编辑: 我刚刚发现我可以使用“android:actionBarTabTextStyle”删除所有大写字母,但它现在从“MyActionBar”获取背景颜色......所以如何在某些层次结构中设置 actionBarTabTextStyle 和 actionBarStyle 列表,该文本被缩小而不是 Caps ,但它不是“MyActionBar”样式的背景。

编辑 2: 我尝试了更多 9patch 图像,它看起来很难看,但我就是无法摆脱蓝线......

【问题讨论】:

  • 嗨,为什么你需要 9-patch 图片?您可以使用视图作为下划线,并使其跨越每个布局。

标签: android tabs actionbarsherlock


【解决方案1】:

我知道这是很久以前问过的,但我花了一段时间试图解决这个问题,所以这是给那些发现这个问题并且仍然想知道答案的人。

首先制作一个包含这个的xml文件:tab_title.xml

<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/action_custom_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My Custom title"
android:textColor="#fff"
android:textSize="18sp"
android:paddingTop="5dp" />

然后在您实例化 ActionBar 的类中,使用此代码设置每个选项卡上的文本。 (本例使用ActionBarSherlock。)

ActionBar bar = getSupportActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

String[] tabNames = {"Tab 1","Tab 2","Tab 3"};

for(int i = 0; i<bar.getTabCount(); i++){
    LayoutInflater inflater = LayoutInflater.from(this);
    View customView = inflater.inflate(R.layout.tab_title, null);

    TextView titleTV = (TextView) customView.findViewById(R.id.action_custom_title);
    titleTV.setText(tabNames[i]);
    //Here you can also add any other styling you want.

    bar.getTabAt(i).setCustomView(customView);
}

希望这对像我一样遇到麻烦的人有所帮助。

//2015 年 1 月 6 日更新

如果您在 android M 预览 中使用 TabLayout 并且想要更改字体,则必须在之前的解决方案中添加一个新的 for 循环,如下所示:

 private void changeTabsFont() {

        ViewGroup vg = (ViewGroup) tabLayout.getChildAt(0);
        int tabsCount = vg.getChildCount();
        for (int j = 0; j < tabsCount; j++) {
            ViewGroup vgTab = (ViewGroup) vg.getChildAt(j);
            int tabChildsCount = vgTab.getChildCount();
            for (int i = 0; i < tabChildsCount; i++) {
                View tabViewChild = vgTab.getChildAt(i);
                if (tabViewChild instanceof TextView) {
                    ((TextView) tabViewChild).setTypeface(Font.getInstance().getTypeFace(), Typeface.NORMAL);
                }
            }
        }
    } 

【讨论】:

  • 我注意到这种方法存在问题。如果您有许多选项卡并且在横向模式下它们不适合 ActionBar,它们将显示在溢出列表中但所选值为空。当您单击此列表的项目时,所有这些视图都会消失。只需确保测试手机上的所有内容(因为在平板电脑上可能有足够的位置放置标签)。 =)
  • 这里描述了我所说的问题-stackoverflow.com/questions/13430542/…
  • 哦,谢谢!在我的项目中,这对我来说是一种非常有效的解决方法,它完成了这项工作。如果您找到更好的方法,请在此处发布!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多