使用夏洛克在操作栏选项卡可以更改字体样式 [英] Change font style in Action Bar Tabs using Sherlock

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

问题描述

我读过这样的:

<一个href="http://stackoverflow.com/questions/9853344/android-customizing-actionbar-sherlock-tabs">Android - 自定义动作条大侦探片

和更多的链接,我已经想通了什么StackedBackground,什么是背景,但我找不到如何删除所有的标签上限,并得到常规字体,或更改字体大小在操作栏中的标签。

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.

旁边的是,我很想改变我的蓝色下划线的颜色,如果我用9补丁图形,出现了类似的情况是上面的链接,我得到下面的文字我的线,但不低于在标签底部,和原来的蓝线依然让我得到两行。

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>

编辑: 我只是想通了,我可以使用机器人:actionBarTabTextStyle删除所有的帽子,但现在需要的背景颜色由MyActionBar......所以,如何设置actionBarTabTextStyle和actionBarStyle列出了一些层次,即文本小型化,而不是在帽的,但它不从MyActionBar风格的背景。

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.

编辑2: 我已经尝试了一些更9patch图像,它看起来很丑,但我就是不能摆脱掉蓝线...

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.

首先要在用这样的一个XML文件:tab_title.xml

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

然后在类,你在实例化你的动作条使用该code设置在每个标签的文本。 (这个例子是使用 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.

//更新2015年1月6日

如果您在机器人M preVIEW使用TabLayout ,然后你要你要添加一个新的循环,像这样的previous解决方案来改变字体:

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

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

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