使用Sherlock更改Action Bar标签中的字体样式

11

我已经阅读了这篇文章:

Android - 定制化actionbar sherlock标签页

我弄清楚了什么是StackedBackground和什么是Background,但我找不到如何取消Tab中的All Caps,以及如何获得常规字体或更改操作栏标签的字体大小。

除此之外,我想要更改我的蓝色下划线颜色,如果我使用9 patch图形,则会出现与上面链接中类似的情况,即文本下方有一条线,但在选项卡底部没有线条,并且原始的蓝色线条仍然存在,因此会出现两条线。

到目前为止,我已经尝试过各种文本大小和颜色,但无济于事:

<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列表的某种层次结构,使文字变小而不是大写,并且不从"MyActionBar"样式中获取背景。

编辑2: 我尝试了一些更多的9patch图像,看起来很丑,但我就是摆脱不了这些蓝线...

UglyBluelines


嗨,你为什么需要在这里使用9-patch图像?你可以使用一个视图作为下划线,并使其跨越每个布局。 - IgorGanapolsky
1个回答

21

我知道这个问题早就被问过了,但我花了一段时间来解决它,所以这里是给任何仍然想知道答案的人。

首先创建一个包含以下内容的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 preview中的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,它们会显示在溢出列表中,但所选值为空。当你点击此列表项时,所有这些视图都会消失。请确保在手机的横向模式下测试一切的外观(因为在平板电脑上可能有足够的空间放置选项卡)。=) - lomza
1
我所讲的问题在这里描述 - http://stackoverflow.com/questions/13430542/actionbarsherlock-tab-loses-custom-view-in-landscape-mode. - lomza
哦,谢谢!在我的项目中,这对我来说非常是一个权宜之计,但它完成了工作。如果你找到更好的方法,请在这里发布! - Marche101

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