使用Sherlock更改操作栏选项卡中的字体样式

Bal*_*yto 10 tabs android actionbarsherlock

我读过这个:

Android - 自定义操作栏sherlock选项卡

还有更多的链接,我已经弄清楚什么是StackedBackground以及什么是Background,但我找不到如何从Tabs中删除All Caps并获得常规字体,或者在操作栏标签中更改字体大小.

除此之外,我喜欢改变我的蓝色下划线的颜色,如果我使用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>
Run Code Online (Sandbox Code Playgroud)

编辑:我只是发现我可以使用"android:actionBarTabTextStyle"删除所有大写字母,但它现在需要"MyActionBar"的背景颜色...所以如何在某些层次结构中设置actionBarTabTextStyle和actionBarStyle列表,该文本缩小了尺寸而不是在Caps中,但它不是"MyActionBar"风格的背景.

编辑2:我已经尝试了一些更多9patch图像,它看起来很丑,但我无法摆脱蓝线......

UglyBluelines

Mar*_*101 20

我知道这是在很久以前被问过的,但我花了一段时间试图解决这个问题,所以这适用于任何发现这个问题且仍想知道答案的人.

首先在其中创建一个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" />
Run Code Online (Sandbox Code Playgroud)

然后在您实例化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);
}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助任何像我一样遇到麻烦的人.

// 2015年6月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);
                }
            }
        }
    } 
Run Code Online (Sandbox Code Playgroud)