【问题标题】:How to set color for both text and icon of a tab in TabLayout when it gets focus获得焦点时如何在 TabLayout 中为选项卡的文本和图标设置颜色
【发布时间】:2016-07-16 12:00:52
【问题描述】:

这是我的 activity_custom_view_icon_text_tabs.xml 布局:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="@dimen/custom_tab_layout_height"
        app:tabMode="fixed"
        app:tabGravity="fill"/>
</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

这是我的 activity_custom_view_icon_text_tabs.xml 代码:http://pastebin.com/raw/h0AN6Mtj

custom_tab.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tab"
android:textColor="@color/colorAccent"
android:textSize="@dimen/tab_label"
android:fontFamily="@string/font_fontFamily_medium"/>

【问题讨论】:

  • 你可以在textview上使用drawable selector,比如stackoverflow.com/questions/5624609/…
  • 使用这个我必须在drawable中有选中和未选中的图标,还有什么办法吗?
  • 这是最好的方法。或者您可以在选项卡选择上操作自定义视图。
  • 但所选标签的文字颜色没有改变,您有什么想法吗?
  • 请看看我的回答。 @Lukie卢克

标签: android android-studio material-design android-tabs


【解决方案1】:

我在我的示例中运行此代码,它运行良好。

您需要进行以下示例中描述的一些更改。

res/drawable/background_selector.xml 下的选项卡选择器背景

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

对于 res/color/background_text_selector.xml 下的文本选择器颜色

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

你的文本视图是这样的:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/background_selector"
    android:textColor="@color/background_text_selector">
</TextView>

参考来自颜色选择器:

https://developer.android.com/guide/topics/resources/color-list-resource.html

对于可绘制选择器:

https://developer.android.com/guide/topics/resources/drawable-resource.html

来自此链接。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-06
    • 2017-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多