簡體   English   中英

選項卡在Sherlock Action Bar中顯示自定義顏色

[英]Tabs text custom color in Sherlock Action Bar

如何根據是否選擇了選項卡更改操作欄中選項卡的顏色?

它應該如下所示:選中時為黑色,未選擇/不活動時為黑色。

我試圖在styles.xml中設置它,但我找不到合適的名稱使它工作。

非常感謝你的幫助!

編輯:我正在使用TabsListener的以下代碼

class MyTabsListener implements TabListener {
        private Fragment fragment;

        public MyTabsListener(Fragment ef) {
            this.fragment = ef;
        }

        public void onTabSelected(Tab tab, FragmentTransaction ft) {
            ft.replace(R.id.realtabcontent, fragment);
            ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        }

        public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        }

        public void onTabReselected(Tab tab, FragmentTransaction ft) {
        }
    }

在此輸入圖像描述

上面的答案是有效的,但它們是不完整的 如果您希望根據選項卡的狀態更改不同的文本顏色 ,則需要執行以下步驟:

  1. 首先, res/values/colors.xml添加到res/values/colors.xml文件中

     <color name="text_tab_selected">#000000</color> <color name="text_tab_unselected">#886C2A</color> 
  2. /res/color下創建一個android xml資源文件,調用它tab_text.xml(或任何你想要的,但跟蹤文件名)。 這個文件需要成為點@Maarek的選擇器。

     <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.androdid.com/apk/res/android"> <item android:state_selected="true" android:color="@color/text_tab_selected" /> <item android:state_active="true" android:color="@color/text_tab_selected"/> <item android:state_selected="false" android:color="@color/text_tab_unselected" /> <item android:state_active="false" android:color="@color/text_tab_unselected"/> </selector> 

    請注意state_active="false"state_active="true"這是真正的交易。

  3. 正如Fatih Kaplan和noloman所解釋的那樣,你必須為你的主題風格添加新的風格。 打開或創建res/values/styles.xml並將您的主題添加到以下行:

     <style name="TabTextColor" parent="@style/Widget.Sherlock.ActionBar.TabText"> <item name="android:textColor">@color/tab_text</item> </style> 
  4. 最后添加到您的應用主題<style name="Theme.yourTheme" parent="@style/Theme.Sherlock">

     <item name="actionBarTabTextStyle">@style/TabTextColor</item> <item name="android:actionBarTextStyle>@style/TabTextColor</item> 
  5. Corolary:請記住將您的主題添加到manifest.xml文件中的Activity。
    如果存在,請記住在每個Api版本的任何樣式文件中重復步驟4。 (res/values-v11/styles.xml, res/values-v16/styles.xml and so on)
    如果您在"android:actionBarStyle"行中收到Lint警告,請將該行替換為以下內容:

     <item name="android:actionBarTabTextStyle" tools:ignore="NewApi">@style/TabTextColor</item> 

標簽捕獲

實際上它很簡單。你應該做的就是定義這樣的屬性

<style name="tabtextcolor" parent="@style/Widget.Sherlock.ActionBar.TabText">
    <item name="android:textColor">@android:color/white</item>
</style>

接着

將這些樣式添加到主題中

<item name="actionBarTabTextStyle">@style/tabtextcolor</item>
<item name="android:actionBarTabTextStyle">@style/tabtextcolor</item>

您可以通過創建顏色資源來完成此操作:

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

    <item android:state_pressed="true" android:color="#ff7a7a7a"/> <!-- pressed -->
    <item android:state_focused="true" android:color="#ff7a7a7a"/> <!-- focused -->
    <item android:state_selected="true" android:color="#ff6955b4"/> <!-- selected -->
    <item android:color="#ff7a7a7a"/> <!-- default -->

</selector>

在你的風格中,你會做這樣的事情。 我使用ActionBarSherlock但你可以很容易地找到非Sherlock應用程序的TabText樣式。

<style name="Theme.StyledActionBar" parent="@style/Theme.Sherlock">
    <item name="actionBarTabTextStyle">@style/StyledActionBarTabText</item>
    <item name="android:actionBarTabTextStyle">@style/StyledActionBarTabText</item>
</style>

<style name="StyledActionBarTabText" parent="@style/Widget.Sherlock.ActionBar.TabText">
    <item name="android:textColor">@color/tab_text</item>
</style>

如果您注冊TabHost.OnTabChanged事件並調用mTabHost.getCurrentTabView()來獲取View,那么view.setBackgroundResource() - 您可以設置背景圖像...

<!-- ActionBar Tab bar style -->
        <item name="actionBarTabBarStyle">@style/Sundio.ActionBarTabs</item>
        <item name="android:actionBarTabBarStyle">@style/Sundio.ActionBarTabs</item>

<style name="Sundio.ActionBarTabs" parent="Widget.Sherlock.Light.ActionBar.TabBar">
        <item name="android:background">@drawable/actionbar_tabs_selector</item>
        <item name="background">@drawable/actionbar_tabs_selector</item>
        <item name="titleTextStyle">@color/brown_text_color</item>
        <item name="android:titleTextStyle">@color/brown_text_color</item>
    </style>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM