2017-01-21 62 views
0

在我的应用程序中实现tablayout,每个选项卡都具有图标和文本。 选择选项卡后,图标和文字应该选择相同的选项卡,并选择具有不同颜色文字和图标的未选选项卡。带有文本和图标的Android 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

回答

0

你可以通过添加一个TabLayout.OnTabSelectedListener,它有三个方法onTabSelected()onTabUnselected()onTabReselected(),你可以用它来改变这两个图标和文本的颜色。这里的link你可以参考它。

0

切换标签文字颜色 在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"/> 

切换标签图标在你fargment /活动添加选择可绘制的每个选项卡。

 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> 
+0

没有什么上tablayout显示。应用上面的代码标签栏后为空白无图标无文字。 – Ravi

+0

上面的代码适用于我 –

+0

使用.setText(“Tab name”)给出标签名称。现在检查更新后的代码文本将出现在图标上正确检查选择器文件 –

相关问题