选项卡选择更改其他选项卡图标并在android中以编程方式更改选项卡文本的颜色

时间:2016-08-23 11:43:33

标签: java android view tabs android-tabhost

我为每个标签开发了带有自定义布局的tabbedview。现在,当我选择一个标签时,其他标签图标应该更改,我想更改其他标签文本的颜色。请帮忙吗?

这是我的代码:

          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) {

        }
    });

}

3 个答案:

答案 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
            }
      } 

答案 1 :(得分:0)

尝试使用此tabSelectedTextColor =“yourColor”,但您在Value->上拥有自己的样式式

   <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" />

答案 2 :(得分:0)

您可以访问androidhive.com http://www.androidhive.info/2015/09/android-material-design-working-with-tabs/

中关于Material Tab布局的精彩文章

只需浏览文章,了解如何更改标签图标和颜色。 要在视图寻呼机项目上更改它,您可以在视图寻呼机中设置PageChangeListener并根据新页面更新选项卡。如果有任何疑问,请随时发表评论。