繁体   English   中英

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

[英]On tab selection change other tabs icons and change the color of the tab text programmatically in android

我已经为每个选项卡开发了具有自定义布局的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) {

        }
    });

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

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

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

您可以在androidhive.com http://www.androidhive.info/2015/09/android-material-design-working-with-tabs/中访问有关Material Tab布局的精彩文章

只需阅读本文,并了解如何更改“标签”图标和颜色。 要在View Pager项上进行更改,可以在View pager中设置PageChangeListener并根据新页面更新选项卡。 如有任何疑问,请随时发表评论。

暂无
暂无

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

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