使用 Sherlock 更改操作栏选项卡中的字体样式 [英] Change font style in Action Bar Tabs using Sherlock

查看:19
本文介绍了使用 Sherlock 更改操作栏选项卡中的字体样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读过这个:

.)

ActionBar bar = getSupportActionBar();bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);字符串[] tabNames = {"标签 1","标签 2","标签 3"};for(int i = 0; i

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

//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++) {查看 tabViewChild = vgTab.getChildAt(i);if (tabViewChild instanceof TextView) {((TextView) tabViewChild).setTypeface(Font.getInstance().getTypeFace(), Typeface.NORMAL);}}}}

I've read this:

Android - customizing actionbar sherlock tabs

And many more links, I've figured out what is StackedBackground and what is Background, but I can't find how to remove All Caps from Tabs and get regular fonts, or change font size in action bar tabs.

Beside that, I'd love to change color of my blue underline, and If I use 9 patch graphics, I get similar situation as on the link above, I get my line below text, but not below on the tab bottom, and the original blue line remains so I get two lines.

So far I have this but I've tried text sizes and all sort of colors, but nothing:

<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>

Edit: I just figured out that I can remove all caps using "android:actionBarTabTextStyle", but it now takes background color from "MyActionBar" ... So How to set actionBarTabTextStyle and actionBarStyle lists in some hierarchy, that text is downsized and not in Caps, but that it does not background from "MyActionBar" style.

Edit 2: I've tried some more 9patch images, and it looks ugly but I just can't get rid off the blue lines...

解决方案

I know this was asked ages ago but I've spent a while trying to sort this out so this is for anybody who has found this question and still wants to know the answer.

First make an xml file with this in it: 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" />

Then in the class where you in instantiate your ActionBar use this code to set the text on each of the tabs. (This example is using 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);
}

Hope this helps anybody who was having trouble like me.

//update 1/6/2015

if you are using TabLayout in android M preview and you want to change the font you have to add a new for loop to the previous solution like this:

 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);
                }
            }
        }
    } 

这篇关于使用 Sherlock 更改操作栏选项卡中的字体样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆