选项卡部件如何改变选项卡底部线条颜色

3

我想改变“选项卡底部线条颜色”,我按照以下步骤进行:
1. 在我的styles.xml文件中,我有:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="android:actionBarTabStyle">@style/ActionBarTabStyle.Dgrey</item>
    </style>

    <style name="ActionBarTabStyle.Dgrey" parent="@android:style/Widget.Holo.ActionBar.TabView">
        <item name="android:background">@drawable/tab_indicator_ab_dgrey</item>
    </style>

</resources>

我有一个 tab_indicator_ab_dgrey,它有以下内容:

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Non focused states -->
    <item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/tab_unselected_dgrey" />
    <item android:state_focused="false" android:state_selected="true"  android:state_pressed="false" android:drawable="@drawable/tab_selected_dgrey" />

    <!-- Focused states -->
    <item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/tab_unselected_focused_dgrey" />
    <item android:state_focused="true" android:state_selected="true"  android:state_pressed="false" android:drawable="@drawable/tab_selected_focused_dgrey" />

    <!-- Pressed -->
    <!--    Non focused states -->
    <item android:state_focused="false" android:state_selected="false" android:state_pressed="true" android:drawable="@drawable/tab_unselected_pressed_dgrey" />
    <item android:state_focused="false" android:state_selected="true"  android:state_pressed="true" android:drawable="@drawable/tab_selected_pressed_dgrey" />

    <!--    Focused states -->
    <item android:state_focused="true" android:state_selected="false" android:state_pressed="true" android:drawable="@drawable/tab_unselected_pressed_dgrey" />
    <item android:state_focused="true" android:state_selected="true"  android:state_pressed="true" android:drawable="@drawable/tab_selected_pressed_dgrey" />
</selector>

3. 在我drawable文件夹(hdpi,mdpi,xhdpi,xxhdpi)中有以下文件:
- tab_selected_dgrey.9.png
- tab_selected_focused_dgrey.9.png
- tab_selected_pressed_dgrey.9.png
- tab_unselected_dgrey.9.png
- tab_unselected_focused_dgrey.9.png
- tab_unselected_pressed_dgrey.9.png

并且在我的drawable文件夹中有:
tab_indicator_ab_dgrey.xml文件

  1. and at the end, this is my manifest.xml:

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="ch.studentgrades.StudentGrades"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
    
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    

我不知道为什么(我跟着教程做了),但当我运行这个应用程序时,它没有做出任何更改,选项卡底部的颜色仍然是蓝色。

1个回答

0

这个答案解决了我的问题。

在设置选项卡后添加以下代码:

TabHost host = (TabHost)view.findViewById(R.id.tab_host);
TabWidget widget = host.getTabWidget();
for(int i = 0; i < widget.getChildCount(); i++) {
    View v = widget.getChildAt(i);

    // Look for the title view to ensure this is an indicator and not a divider.
    TextView tv = (TextView)v.findViewById(android.R.id.title);
    if(tv == null) {
        continue;
    }
    v.setBackgroundResource(R.drawable.tab_indicator_ab_dgrey);
}

那么,您可以从您的styles.xml中删除以下代码:

<item name="android:actionBarTabStyle">@style/ActionBarTabStyle.Dgrey</item>

并且

<style name="ActionBarTabStyle.Dgrey" parent="@android:style/Widget.Holo.ActionBar.TabView">
    <item name="android:background">@drawable/tab_indicator_ab_dgrey</item>
</style>

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