简体   繁体   English

在所选标签中更改文本颜色

[英]Change text color in selected tab

I have a problem with tabs in my app. 我的应用程序中的标签有问题。 When I select a tab, I want to change its icon and text color. 选择选项卡时,我想更改其图标和文本颜色。 When I change to another tab, the icon and text color need to change to the neutral color. 当我更改为另一个选项卡时,图标和文本的颜色需要更改为中性色。

I tried to do this, but while the icon does change, the text color stays the same. 我尝试这样做,但是当图标确实改变时,文本颜色保持不变。

final TabLayout tabs = (TabLayout) findViewById(R.id.tabs);
tabs.addTab(tabs.newTab().setIcon(R.mipmap.destacados_act).setText("Destacados"));
tabs.setSelectedTabIndicatorColor(Color.rgb(255,170,0));
tabs.addTab(tabs.newTab().setIcon(R.mipmap.secciones).setText("Secciones"));
tabs.addTab(tabs.newTab().setIcon(R.mipmap.descargas).setText("Descargas"));

final ViewPager view_pager = (ViewPager) findViewById(R.id.pager);
final ViewPagerAdapterPrincipal adapter = new ViewPagerAdapterPrincipal(getSupportFragmentManager(), tabs.getTabCount());
view_pager.setAdapter(adapter);
view_pager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabs));

tabs.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        view_pager.setCurrentItem(tab.getPosition());

        switch (tab.getPosition()) {
            case 0:
                tab.setIcon(R.mipmap.destacados_act);
                tabs.setSelectedTabIndicatorColor(Color.rgb(255,170,0));
                break;
            case 1:
                tab.setIcon(R.mipmap.secciones_act);
                tabs.setSelectedTabIndicatorColor(Color.rgb(0,255,255));
                break;
            case 2:
                tab.setIcon(R.mipmap.descargas_act);
                tabs.setSelectedTabIndicatorColor(Color.rgb(170,255,0));
                break;
        }
    }
    public void onTabUnselected(TabLayout.Tab tab) {

        switch (tab.getPosition()) {
            case 0:
                tab.setIcon(R.mipmap.destacados);
                break;
            case 1:
                tab.setIcon(R.mipmap.secciones);
                break;
            case 2:
                tab.setIcon(R.mipmap.descargas);
                break;
        }
    }
    public void onTabReselected(TabLayout.Tab tab) {
    }
});

Here is my code for the adapter 这是我的适配器代码

public class ViewPagerAdapterPrincipal extends FragmentStatePagerAdapter {

    int numOfTabs;

    public ViewPagerAdapterPrincipal(FragmentManager fm, int numOfTabs) {

        super(fm);
        this.numOfTabs = numOfTabs;
    }

    public Fragment getItem(int position) {

        switch(position){
            case 0 :
                DestacadosPrincipal tab1 = new DestacadosPrincipal();
                return tab1;
            case 1 :
                Secciones tab2 = new Secciones();
                return tab2;
            case 2 :
                Descargas tab3 = new Descargas();
                return tab3;
            default:
                return null;
        }
    }

    public int getCount() {
        return numOfTabs;
    }
}

The problem comes in OnTabUnselected if I erase 如果我擦除问题出在OnTabUnselected
tab.setIcon(); The text color is fine but obviously the icon doesn't change. 文字颜色很好,但显然图标没有改变。

You can set a style to the TextView on your tabs using a selector 您可以使用选择器在选项卡上为TextView设置样式

This assumes you've used a custom tab layout containing a TextView with style="@style/tabText". 假设您使用的自定义选项卡布局包含带有style =“ @ style / tabText”的TextView。

values/styles.xml values / styles.xml

<style name="tabText">
    <item name="android:textColor">@drawable/text_selector_tab</item>
    <item name="android:textSize">@dimen/fontTab</item>
    <item name="android:textAllCaps">true</item>
</style>

then set the textColor using a selector: 然后使用选择器设置textColor:

drawable/text_selector_tab.xml drawable / text_selector_tab.xml

<?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/colorSelected" />
    <item android:color="@color/colorDeselected" />
</selector>

then set your color attributes for selected and unselected states (ie shown here as colorSelected and colorDeselected. 然后为选定和未选定状态设置颜色属性(即,此处显示为colorSelected和colorDeselected。

You can customize your tabs with a custom layout: 您可以使用自定义布局自定义标签:

layoutTab = (LinearLayout)inflater.inflate(R.layout.layout_tab, null);
TabLayout.Tab tab = mTabLayout.newTab();
tab.setCustomView(layoutTab);
mTabLayout.addTab(tabHome);

layout/layout_tab.xml layout / layout_tab.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="wrap_content"
    android:orientation="horizontal"
    android:gravity="center"
    android:clipToPadding="false">

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        style="@style/tabText" />

 </LinearLayout>

this is how the TextView in the tab gets tied to the style. 这就是选项卡中的TextView与样式绑定的方式。 You can dig into the standard tab layout XML in the platform SDK to see how the icon and text work and adjust this layout accordingly. 您可以在平台SDK中挖掘标准的选项卡布局XML,以查看图标和文本的工作方式并相应地调整此布局。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM