如何更改TabWidget的背景颜色(仅限xml)

时间:2014-12-08 19:12:49

标签: android android-layout

这个问题已被多次询问,但我无法找到适用于我的xml样式的明确解决方案。所以在其他示例中使用了操作栏中的选项卡,所以再次,它不是我的情况。

在清单中:

android:theme="@style/AppTheme"

在style.xml中:

<style name="AppBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <item name="android:actionBarTabStyle">@style/MyActionBarTabs</item>
        <item name="actionBarTabStyle">@style/MyActionBarTabs</item>
    </style>

<style name="MyActionBarTabs" parent="@style/Widget.AppCompat.ActionBar.TabView">
        <item name="android:background">@drawable/actionbar_tab_indicator</item>
        <item name="background">@drawable/actionbar_tab_indicator</item>
    </style>

在actionbar_tab_indicator.xml中:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<!-- STATES WHEN BUTTON IS NOT PRESSED -->

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

    <!-- Focused states (such as when focused with a d-pad or mouse hover) -->
    <item android:state_focused="true" android:state_selected="false"
          android:state_pressed="false"
          android:drawable="@color/red_spec" />
    <item android:state_focused="true" android:state_selected="true"
          android:state_pressed="false"
          android:drawable="@color/red_spec" />


<!-- STATES WHEN BUTTON IS PRESSED -->

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

    <!-- Focused states (such as when focused with a d-pad or mouse hover) -->
    <item android:state_focused="true" android:state_selected="false"
          android:state_pressed="true"
          android:drawable="@color/red_spec" />
    <item android:state_focused="true" android:state_selected="true"
          android:state_pressed="true"
          android:drawable="@color/red_spec" />
</selector>

使用上述样式组合根本不起作用。它没有改变任何东西。

如果我在下面使用此样式,它仅适用于更改字体样式但不适用于背景:

 <style name="AppTheme" parent="AppBaseTheme">
        <item name="android:tabWidgetStyle">@style/CustomTabWidget</item>
            </style>
 <style name="CustomTabWidget" parent="@android:style/Widget.TabWidget">
        <item name="android:textAppearance">@style/CustomTabWidgetText</item>
        <item name="android:background">@color/red_overlay_spec</item>
    </style>

    <style name="CustomTabWidgetText" parent="@android:style/TextAppearance.Widget.TabWidget">
        <item name="android:textSize">25sp</item>
        <item name="android:textStyle">bold</item>
    </style>

这是我用来基于TabHost进行片段布局的片段:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center_horizontal"
    android:orientation="vertical"
    android:background="@color/core_blue_bgr" >

    <TabHost
        android:id="@android:id/tabhost"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"

         >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0"
                android:orientation="horizontal" />

            <!-- tab host should have frameLayout with this ID 
            but is not use for content-->
            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_weight="0" />
            <!-- the real content is put here -->
            <FrameLayout
                android:id="@+android:id/realtabcontent"
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:layout_weight="1" />
        </LinearLayout>
    </TabHost>
</LinearLayout>

1 个答案:

答案 0 :(得分:0)

在第一个Tab背景颜色变化中只有一次,但在我选择其他标签

后该颜色没有变化

你可能只设置一次(仅限onCreate) 尝试在setOnTabChangedListener上设置它,如下所示

tabHost.setOnTabChangedListener(new OnTabChangeListener() {

@Override
public void onTabChanged(String tabId) {
setTabColor(tabHost);

}