2014-10-02 106 views
0

我在我的應用程序中發生的一個奇怪的UI錯誤,只在Android 4.1.2(真實設備)上拉我的頭髮。ActionBar TabBar上的黑色顏色

錯誤在於活動選項卡上的背景顏色是黑色的(請參見下面的截圖) 它應該是:活動(選定)選項卡的白色背景顏色,非選定選項卡的灰色背景。

儘管在我的styles.xml文件中,我清楚地設置了一個狀態列表,當該選項卡處於活動狀態時,它可以用白色背景繪製,並且在Android 4.2.2及更高版本上完美運行。

enter image description here

這裏是我的styles.xml:

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


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

     <!-- This is a White background --> 
     <item name="android:actionBarTabBarStyle">@style/TabBar</item> 


     <item name="android:actionBarTabStyle">@style/MyActionBarTabs</item> 



    </style> 

在styles.xml標籤欄的定製:

<style name="TabBar" parent="android:style/Widget.Holo.Light.ActionBar.TabBar"> 
     <!-- This is a White background --> 
     <item name="android:background">@color/white</item> 
    </style> 


    <!-- ActionBar tabs styles --> 
    <style name="MyActionBarTabs" parent="android:style/Widget.Holo.Light.ActionBar.TabView"> 
     <!-- tab indicator --> 
     <item name="android:background">@drawable/tab_bar_background</item> 
    </style> 


    <style name="TabText" parent="android:style/Widget.Holo.Light.ActionBar.TabText"> 
     <!-- This is a WHITE tab color --> 
     <item name="android:textColor">@color/selector_tab_text</item> 
     <item name="android:textAllCaps">false</item> 
    </style> 

,這裏是我的:tab_bar_background.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <!-- UNSELECTED TAB STATE --> 
    <item android:state_selected="false" android:state_pressed="false"> 
     <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
      <!-- Bottom indicator color for the UNSELECTED tab state --> 
      <!-- Tab background color for the SELECTED tab state --> 
      <item> 
       <shape> 
        <solid android:color="#d0d0d0"/> 
       </shape> 
      </item> 

     </layer-list> 
    </item> 
    <!-- SELECTED TAB STATE --> 
    <item android:state_selected="true" android:state_pressed="false"> 
     <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
      <!-- Tab background color for the SELECTED tab state --> 
      <item> 
       <shape> 
        <solid android:color="@color/white"/> 
       </shape> 
      </item> 
      <!-- Bottom indicator color for the SELECTED tab state --> 
      <item android:top="-5dp" android:left="-5dp" android:right="-5dp"> 
       <shape android:shape="rectangle"> 
        <stroke android:color="#309CB9" android:width="3dp"/> 
       </shape> 
      </item> 
     </layer-list> 
    </item> 
</selector> 

我加上我的活動延伸FragmentActivity並實現TabListener這樣的標籤:

//sets the tabs 
     actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 

正如我提到的這個代碼工作完全在Android 4.2.2版(白色背景爲選定的選項卡)

我錯過了什麼嗎?

謝謝你的時間。

回答

0

謝謝大家的幫助。 我設法讓我的標籤欄用這個代碼(谷歌android版權)代替我的列表繪製正常工作:

<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_holo" /> 
    <item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/tab_selected_holo" /> 

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

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

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

提拉可在這裏(xxhdpi) https://github.com/android/platform_frameworks_base/tree/master/core/res/res/drawable-xxhdpi (谷歌的代碼,並繪製)

返回問題自我: 爲什麼舊代碼在某些設備上工作而不在其他人上,這仍然是一個謎。

在關閉主題前添加一件事:通過從舊列表中刪除代碼的這一部分drawable它的工作=選定(激活)選項卡上的白色背景,但當然沒有SELECTED選項卡狀態的底部指示器顏色!

<item android:top="-5dp" android:left="-5dp" android:right="-5dp"> 
       <shape android:shape="rectangle"> 
        <stroke android:color="#309CB9" android:width="3dp"/> 
       </shape> 
      </item> 

我假設具有在同一層列表兩項打破了東西,創造了「在一些設備上的」預4.2的錯誤行爲。X

0

一些三星設備的顏色混亂了。你在測試嗎?如果是這樣,請嘗試將所選標籤的顏色從白色更改爲黑色。

+0

感謝特奧多爾爲你的答案,是的,它是三星...但我不明白如何將顏色從白色變爲黑色將解決這個問題。乾杯 – moujib 2014-10-02 17:11:25

+0

在某些設備上,他們的顏色混亂了。 #000000是白色的,#ffffff是黑色的,中間的都是錯誤的。您可能正在使用三星Android這樣混亂的設備進行測試。 – 2014-10-03 15:15:29

0

這是你下面指示燈的顏色是什麼樣子:

<item android:top="-5dp" android:left="-5dp" android:right="-5dp"> 
      <shape android:shape="rectangle"> 
       <stroke android:color="#309CB9" android:width="3dp"/> 
      </shape> 
     </item> 

您需要添加固體透明填充,它會解決這個問題,像這樣的:

<item android:top="-5dp" android:left="-5dp" android:right="-5dp"> 
      <shape android:shape="rectangle"> 
       **<solid android:color="@android:color/transparent"/>** 
       <stroke android:color="#309CB9" android:width="3dp"/> 
      </shape> 
     </item>