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

标签 android tabs actionbarsherlock

我读过这个:

Android - customizing actionbar sherlock tabs

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

除此之外,我想更改蓝色下划线的颜色,如果我使用 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 列表,该文本被缩小而不是在大写字母中,但它不是来自“MyActionBar”样式的背景。

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

UglyBluelines

最佳答案

我知道很久以前就有人问过这个问题,但我花了一段时间试图解决这个问题,所以这是为找到这个问题但仍想知道答案的任何人准备的。

首先制作一个包含这个的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);
                }
            }
        }
    } 

关于android - 使用 Sherlock 更改操作栏选项卡中的字体样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12665186/

相关文章:

java - AsyncTaks 的受控执行

perl - perl 中的选项卡扩展

javascript - (jquery tab ui) 如何防止无限制的 jquery 选项卡创建

android - ActionBar 选项卡删除填充和分隔符

android - 对 ActionBarSherlock 中 ShareAction 提供程序的弹出窗口进行主题化

android - Samsung Galaxy S3 版本 4.1.1 中的 Camera Force Closing 问题

android - "Failed to find mounted volume for"

android - Gradle 错误 : Android Studio, ActionbarSherlock 和 SlidingMenu

java - AlarmManager 和服务无法在我的应用程序上运行

android - onCreateOptionsMenu 在 onCreate android 之前调用