如何在获得焦点时为 TabLayout 中的选项卡的文本和图标设置颜色
How to set color for both text and icon of a tab in TabLayout when it gets focus
这是我的 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"/>
i 运行 这个代码在我的示例中,它工作正常。
您需要进行以下示例中描述的一些更改。
用于 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
来自此链接。
这是我的 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"/>
i 运行 这个代码在我的示例中,它工作正常。
您需要进行以下示例中描述的一些更改。
用于 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
来自此链接。