2016-08-23 144 views
0

以编程方式更改选项卡文本的颜色我已经开发tabbedview与每个单独的选项卡的自定义布局。现在当我选择一个选项卡其他选项卡图标应该更改,我想更改其他标签文本的颜色。请帮忙?在选项卡选择更改其他选项卡图标,并在android

这里是我的代码:

  final TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout); 
     tabLayout.addTab(tabLayout.newTab().setCustomView(R.layout.custom_tab_one)); 
     tabLayout.addTab(tabLayout.newTab().setCustomView(R.layout.custom_tab_two)); 
     tabLayout.addTab(tabLayout.newTab().setCustomView(R.layout.custom_tab_three)); 
     tabLayout.addTab(tabLayout.newTab().setCustomView(R.layout.custom_tab_four)); 


    final ViewPager viewPager = (ViewPager) findViewById(R.id.pager); 
    final TabViewAdapter adapter = new TabViewAdapter(getSupportFragmentManager(),tabLayout.getTabCount()); 
    viewPager.setAdapter(adapter); 

    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout)); 
    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { 
     @Override 
     public void onTabSelected(TabLayout.Tab tab) { 
      viewPager.setCurrentItem(tab.getPosition()); 

      } 

     @Override 
     public void onTabUnselected(TabLayout.Tab tab) { 


     } 

     @Override 
     public void onTabReselected(TabLayout.Tab tab) { 

     } 
    }); 

} 
+0

请加上您的代码 –

回答

0
// - set a tab listener to your tabLayout 
    tabLayout.setOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewPager) { 
      @Override 
      public void onTabUnselected(TabLayout.Tab tab) { 
       super.onTabUnselected(tab); 
      } 
      // - this method will get execute if you select some tab 
      @Override 
      public void onTabSelected(TabLayout.Tab tab) { 
       super.onTabSelected(tab); 
       // - here change the color or image of your tab which is selected and of unselected also 
      } 
     } 
+0

您能解释一下您的代码吗......我没有得到这个 –

+0

答案更新了评论 – Rahul

0

尝试使用此tabSelectedTextColor = “yourColor” 和而你自己对价值风格>风格

<android.support.design.widget.TabLayout 
     android:id="@+id/tabs" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@color/white" 
     app:tabMode="scrollable" 
     app:tabGravity="fill" 
     app:tabSelectedTextColor="@color/black" 
     app:tabTextColor="@color/red" /> 
0

您可以访问这个完美的事情文章在androidhive.com上的Material Tab layout http://www.androidhive.info/2015/09/android-material-design-working-with-tabs/

只要阅读文章并学习ho w更改选项卡图标和颜色。 要在查看寻呼机项目中进行更改,您可以在查看寻呼机中设置PageChangeListener,并根据新页面更新选项卡。如果还有任何疑问,请随时发表评论。

相关问题