在Fragment Activity中设置Android选项卡的背景颜色

时间:2014-06-16 11:55:39

标签: android android-fragments

我尝试将现有代码从TabActivity迁移到FragmentActivity,因为TabActivity现已弃用。

在此之前,我使用此代码更改每个标签的背景颜色:

TabWidget widget = tabHost.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);
            tv.setTextColor(Color.parseColor("#E64260"));
            tv.setTextSize(15);
            tv.setTypeface(robotolight);

            v.setBackgroundResource(R.drawable.tab_selector);
        }

tab_selector.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android">

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

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

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

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

我不知道如何为Fragment Activity调整此代码。你能帮帮我吗?

更新:

<?xml version="1.0" encoding="utf-8"?>
<!-- File created by the Android Action Bar Style Generator

     Copyright (C) 2011 The Android Open Source Project
     Copyright (C) 2012 readyState Software Ltd

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<resources>

    <style name="Theme.Detail_bar_style" parent="@android:style/Theme.Holo.Light">
        <item name="android:actionBarItemBackground">@drawable/selectable_background_detail_bar_style</item>
        <item name="android:popupMenuStyle">@style/PopupMenu.Detail_bar_style</item>
        <item name="android:dropDownListViewStyle">@style/DropDownListView.Detail_bar_style</item>
        <item name="android:actionBarTabStyle">@style/ActionBarTabStyle.Detail_bar_style</item>
        <item name="android:actionDropDownStyle">@style/DropDownNav.Detail_bar_style</item>
        <item name="android:actionBarStyle">@style/ActionBar.Solid.Detail_bar_style</item>
        <item name="android:actionModeBackground">@drawable/cab_background_top_detail_bar_style</item>
        <item name="android:actionModeSplitBackground">@drawable/cab_background_bottom_detail_bar_style</item>
        <item name="android:actionModeCloseButtonStyle">@style/ActionButton.CloseMode.Detail_bar_style</item>


    </style>

    <style name="ActionBar.Solid.Detail_bar_style" parent="@android:style/Widget.Holo.Light.ActionBar.Solid">
        <item name="android:background">@drawable/ab_background_textured_detail_bar_style</item>
        <item name="android:backgroundStacked">@drawable/ab_stacked_solid_detail_bar_style</item>
        <item name="android:backgroundSplit">@drawable/ab_background_textured_detail_bar_style</item>
        <item name="android:progressBarStyle">@style/ProgressBar.Detail_bar_style</item>
    </style>

    <style name="ActionBar.Transparent.Detail_bar_style" parent="@android:style/Widget.Holo.Light.ActionBar">
        <item name="android:background">@drawable/ab_transparent_detail_bar_style</item>
        <item name="android:progressBarStyle">@style/ProgressBar.Detail_bar_style</item>
    </style>

    <style name="PopupMenu.Detail_bar_style" parent="@android:style/Widget.Holo.Light.ListPopupWindow">
        <item name="android:popupBackground">@drawable/menu_dropdown_panel_detail_bar_style</item>  
    </style>

    <style name="DropDownListView.Detail_bar_style" parent="@android:style/Widget.Holo.Light.ListView.DropDown">
        <item name="android:listSelector">@drawable/selectable_background_detail_bar_style</item>
    </style>

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

    <style name="DropDownNav.Detail_bar_style" parent="@android:style/Widget.Holo.Light.Spinner">
        <item name="android:background">@drawable/spinner_background_ab_detail_bar_style</item>
        <item name="android:popupBackground">@drawable/menu_dropdown_panel_detail_bar_style</item>
        <item name="android:dropDownSelector">@drawable/selectable_background_detail_bar_style</item>
    </style>

    <style name="ProgressBar.Detail_bar_style" parent="@android:style/Widget.Holo.Light.ProgressBar.Horizontal">
        <item name="android:progressDrawable">@drawable/progress_horizontal_detail_bar_style</item>
    </style>

    <style name="ActionButton.CloseMode.Detail_bar_style" parent="@android:style/Widget.Holo.Light.ActionButton.CloseMode">
        <item name="android:background">@drawable/btn_cab_done_detail_bar_style</item>
    </style>

    <!-- this style is only referenced in a Light.DarkActionBar based theme -->
    <style name="Theme.Detail_bar_style.Widget" parent="@android:style/Theme.Holo">
        <item name="android:popupMenuStyle">@style/PopupMenu.Detail_bar_style</item>
        <item name="android:dropDownListViewStyle">@style/DropDownListView.Detail_bar_style</item>
    </style>

</resources>

1 个答案:

答案 0 :(得分:1)

您可以通过styles.xml

实现目标
<?xml version="1.0" encoding="utf-8"?>
<resources>
  <style name="MyTheme" parent="@android:style/Theme.Holo.Light">
    <item name="android:actionBarStyle">@style/MyTheme.ActionBarStyle</item>
  </style>

  <style name="MyTheme.ActionBarStyle"parent="@android:style/Widget.Holo.Light.ActionBar">
    <item name="android:titleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
  </style>

  <style name="MyTheme.ActionBar.TitleTextStyle" parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Title">
    <item name="android:textColor">@color/red</item>
  </style>
</resources>

如果您使用的是ActionBarSherlock,请尝试使用此功能 ActionBarStyle Generator

为了我自己的目的,我提到了this link。想与你分享同样的事情。