繁体   English   中英

带有文本和图标的Android TabLayout更改选定选项卡上的文本和图标的颜色

[英]Android TabLayout With Text and Icons Change color of both text and Icon on selected tab

在我的应用程序中,实现tablayout,每个选项卡既包含图标又包含文本。 选择选项卡后,应选择同一选项卡和未选择的选项卡的图标和文本,并使用不同颜色的文本和图标。

以下是我的代码,用于实现标签页布局,但无法在标签页选择中更改文本颜色和图标颜色。

private void setupTabIcons() {

    TextView tabOne = (TextView) LayoutInflater.from(mContext).inflate(R.layout.custome_tab_with_icon, null);
    tabOne.setText("Home");
    tabOne.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.selector_home, 0, 0);
    tabLayout.getTabAt(0).setCustomView(tabOne);

    TextView tabTwo = (TextView) LayoutInflater.from(mContext).inflate(R.layout.custome_tab_with_icon, null);
    tabTwo.setText("Search");
    tabTwo.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.selector_search, 0, 0);
    tabLayout.getTabAt(1).setCustomView(tabTwo);

    TextView tabThree = (TextView) LayoutInflater.from(mContext).inflate(R.layout.custome_tab_with_icon, null);
    tabThree.setText("WishList");
    tabThree.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.selector_wishlist, 0, 0);
    tabLayout.getTabAt(2).setCustomView(tabThree);

    TextView tabFour = (TextView) LayoutInflater.from(mContext).inflate(R.layout.custome_tab_with_icon, null);
    tabFour.setText("Cart");
    tabFour.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.selector_cart, 0, 0);
    tabLayout.getTabAt(3).setCustomView(tabFour);

    TextView tabFive = (TextView) LayoutInflater.from(mContext).inflate(R.layout.custome_tab_with_icon, null);
    tabFive.setText("Account");
    tabFive.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.selector_accounts, 0, 0);
    tabLayout.getTabAt(4).setCustomView(tabFive);

}

选择选项卡时,请帮助如何更改文本颜色和图标。

TIA

切换选项卡文本颜色在您的xml中,将行app:tabTextColorapp:tabSelectedTextColor到TabLayout。

        <android.support.design.widget.TabLayout
            android:layout_width="match_parent"
            app:tabTextColor="#000000"
            app:tabSelectedTextColor="#FFFFFF"
            android:layout_height="wrap_content"/>

切换标签图标在您的活动/活动中,添加可绘制到每个标签的选择器。

        tabLayout = (TabLayout) findViewById(R.id.tab_layout);
        //Set selector drawable to each tab
        tabLayout.addTab(tabLayout.newTab().setText("Warm Up").setIcon(ResourcesCompat.getDrawable(getResources(), R.drawable.selector_warmup_icon,null)));
        tabLayout.addTab(tabLayout.newTab().setText("Exercise").setIcon(ResourcesCompat.getDrawable(getResources(), R.drawable.selector_exercise_icon, null)));
        tabLayout.addTab(tabLayout.newTab().setText("Rest").setIcon(ResourcesCompat.getDrawable(getResources(), R.drawable.selector_rest_icon, null)));
        tabLayout.addTab(tabLayout.newTab().setText("Success").setIcon(ResourcesCompat.getDrawable(getResources(), R.drawable.selector_success_icon, null)));

selector_warmup_icon.xml(应该是)

 <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/ic_human_white_48dp" android:state_selected="true"/>
    <item android:drawable="@drawable/ic_human_grey600_24dp" android:state_selected="false"/>

</selector>

您可以通过添加TabLayout.OnTabSelectedListener来做到这TabLayout.OnTabSelectedListener ,它具有onTabSelected()onTabUnselected()onTabReselected()三种方法,可用于更改图标和文本的颜色。 这是您可以参考的链接

您可以将“ 颜色状态列表”资源用于文本颜色以及图标的色彩。 我认为android:state_selected应该可以工作。

暂无
暂无

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

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