【问题标题】:Why can I not apply a different textColor to tabs?为什么我不能将不同的 textColor 应用于选项卡?
【发布时间】:2011-11-25 15:34:25
【问题描述】:

我一直在制作自己的自定义选项卡,我想根据是否选中来更改选项卡的 textColor。

我在“values”中创建了自己的样式,旨在仅更改 textColor:

<style name="TabTextStyle" parent="@android:attr/tabWidgetStyle">
    <item name="android:textColor">@drawable/tab_text_color</item>
</style>

...基于考虑“state_selected”的drawable:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:textColor="#000000"
        android:state_selected="true"/> 
    <item        
        android:textColor="#FFFFFF"/>
</selector>

然后我在位于布局中的 TabIndicator 中设置了 TextView 的样式:

<TextView android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true"
    style="@style/TabTextStyle"/> 

这会导致我的应用崩溃:\

  • 如果我将 TabTextStyle 设置为固定颜色,一切都很好。
  • 如果我将样式设置为 ?@android:attr/tabWidgetStyle... 一切正常,但颜色未按我想要的方式定义。

有人有什么想法吗?

另外,我尝试在选择器内设置颜色,在此我根据 state_selected 选择图标。这会导致图标消失。

我只能假设我错过了一些事情并且我做错了这一切,但这似乎是教程所说的那样。

希望有人能帮忙 :) 谢谢!

【问题讨论】:

    标签: android tabs styles


    【解决方案1】:

    尝试执行以下操作

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
     <!-- When selected, use grey -->
     <item android:drawable="@color/black"
          android:state_selected="true" />
     <!-- When not selected, use white-->
     <item android:drawable="@color/white" />
    </selector>
    

    定义你自己的颜色资源或使用android提供的资源——“@android:color/black”等。

    【讨论】:

    • 啊!现在看起来很明显(我认为该项目可能必须是可绘制的) - 我希望我可以远程访问来尝试这个。星期一会第一件事调查它并让你知道。谢谢。
    【解决方案2】:

    有效!问题是我需要选择器是 ColorSelector - 不使用 textColors 或 drawables。

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

    【讨论】:

      猜你喜欢
      • 2020-10-18
      • 2017-01-29
      • 2013-06-02
      • 1970-01-01
      • 2022-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-18
      相关资源
      最近更新 更多