【问题标题】:TabWidget change tab bottom line colorTabWidget 更改选项卡底线颜色
【发布时间】:2015-01-19 14:52:53
【问题描述】:

我想更改“标签底线颜色”,我按照以下步骤操作:
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>

2。在我的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。在我的可绘制文件夹(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

在我的可绘制文件夹中,我有:
tab_indicator_ab_dgrey.xml 文件

  1. 最后,这是我的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>
    

我不知道为什么(我按照教程操作)当我运行应用程序时没有改变任何东西,标签行颜色保持蓝色。

【问题讨论】:

    标签: android android-tabhost


    【解决方案1】:

    answer 解决了我的问题。

    设置标签后添加此代码:

    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>
    

    【讨论】:

      猜你喜欢
      • 2013-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-18
      相关资源
      最近更新 更多