2014-10-02 47 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>