【问题标题】:How to change tab background on viewpagerindicator tabs?如何更改 viewpagerindicator 选项卡上的选项卡背景?
【发布时间】:2012-10-13 03:31:25
【问题描述】:

我正在使用 Jake Wharton 的 ViewpagerIndicator 库。我将选项卡代码与 ActionBarSherlock 库结合使用。一切正常,但我正在尝试设置标签背景的样式,但不知道如何。 我想要一个带有深色标签和浅色片段(标签内容)的深色操作栏

我使用的基本主题是 Theme.Sherlock.Light.DarkActionBar。我通过使其成为设置选项卡属性(如文本颜色、指示器颜色等)的样式的父级来扩展此样式。这会导致操作栏变暗、选项卡变亮和片段变亮。

我找不到任何会改变标签本身背景的东西。我可以更改它的唯一方法是将整个应用程序更改为黑暗(使用 Theme.Sherlock)。到目前为止,这是我的代码:

<style name="vpiTheme" parent="Theme.Sherlock.Light.DarkActionBar">
    <item name="vpiTabPageIndicatorStyle">@style/CustomTabPageIndicator</item>
</style>

<style name="CustomTabPageIndicator" parent="Widget.TabPageIndicator">
    <item name="android:textColor">#FF000000</item>
    <item name="android:paddingTop">6dp</item>
    <item name="android:paddingBottom">6dp</item>
    <item name="android:paddingLeft">16dip</item>
    <item name="android:paddingRight">16dip</item>
    <item name="android:maxLines">2</item>
</style>

【问题讨论】:

    标签: android colors background tabs viewpagerindicator


    【解决方案1】:

    由于 9-patch 可绘制对象大多是透明的,因此只需向整个 ViewPagerIndicator 添加背景颜色即可更改选项卡的颜色,如下所示:

    <com.viewpagerindicator.TabPageIndicator
        android:id="@+id/indicator"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:background="#333333" />
    

    这样您就可以继续使用基于Theme.Sherlock.Light.DarkActionBar 的主题,而无需创建新的drawable。

    【讨论】:

      【解决方案2】:

      首先在你的项目中添加一个drawable,下面的那个叫做tab_indicator.xml:

      <?xml version="1.0" encoding="utf-8"?>
      
      <selector xmlns:android="http://schemas.android.com/apk/res/android">
      <item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@android:color/black" />
      <item android:state_focused="false" android:state_selected="true"  android:state_pressed="false" android:drawable="@android:color/white" />
      <item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@android:color/black" />
      <item android:state_focused="true" android:state_selected="true"  android:state_pressed="false" android:drawable="@android:color/white" />
      </selector>
      

      然后以你的风格引用你的drawable:

      <item name="android:background">@drawable/tab_indicator</item>
      

      这将使活动选项卡具有白色背景,而其他选项卡具有黑色。

      【讨论】:

      • 这会代替彩色指示器吗?
      • 是的,没错。我忘记了我的目标是删除指标。 stackoverflow.com/questions/10364045/… 的答案应该是适用的,因为默认指示器/背景是使用 9 个补丁图像完成的。
      猜你喜欢
      • 2018-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-11
      • 2013-04-01
      • 2022-09-28
      相关资源
      最近更新 更多