【问题标题】:Black color on ActionBar TabBarActionBar TabBar 上的黑色
【发布时间】:2014-10-02 13:57:50
【问题描述】:

我的应用程序中出现一个奇怪的 UI 错误,该错误仅在 android 4.1.2(真实设备)上出现。

错误是活动选项卡上的背景颜色是黑色(见下面的截图) 它应该是:活动(选定)选项卡的白色背景色和非活动未选定选项卡的灰色背景。

虽然在我的styles.xml文件中,我清楚地设置了当标签处于活动状态的白色背景的状态列表,并且它在Android版本4.2.2及更高版本上完美地工作。

这是我的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 上完美运行(所选标签的白色背景)

我错过了什么吗?

感谢您的宝贵时间。

【问题讨论】:

    标签: android android-fragments tabs android-actionbar tabbar


    【解决方案1】:

    这是你底部指示器颜色的样子:

    <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>
    

    【讨论】:

      【解决方案2】:

      某些三星设备的颜色混乱。你在做这样的测试吗?如果是这样,请尝试将所选标签的颜色从白色更改为黑色。

      【讨论】:

      • 感谢 Teodor 的回答,是的,它是三星的...但我不明白如何将颜色从白色更改为黑色将解决问题。干杯
      • 在某些设备上,它们的颜色混乱了。 #000000 是白色的,#ffffff 是黑色的,中间的都是错误的。您可能正在使用三星 Android 如此混乱的设备进行测试。
      【解决方案3】:

      感谢大家的帮助。 通过用此代码替换我的可绘制列表(Google 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>
      

      drawable 在这里可用 (xxhdpi) https://github.com/android/platform_frameworks_base/tree/master/core/res/res/drawable-xxhdpi (谷歌的代码和drawable)

      回到问题本身: 为什么相同的旧代码在某些设备上有效而在其他设备上无效,这仍然是一个谜。

      在结束主题之前要添加的一件事:通过从旧的可绘制列表中删除这部分代码,它起作用 = 选定(激活)选项卡上的白色背景,但当然没有选定选项卡状态的底部指示器颜色!

      <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 之前的“某些设备上”造成错误行为

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-10-11
        • 1970-01-01
        • 2014-07-22
        • 1970-01-01
        • 2020-07-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多