【问题标题】:On tab selection change other tabs icons and change the color of the tab text programmatically in android在选项卡选择上更改其他选项卡图标并在 android 中以编程方式更改选项卡文本的颜色
【发布时间】:2016-08-23 11:43:32
【问题描述】:

我已经为每个单独的选项卡开发了带有自定义布局的选项卡视图。现在,当我选择一个选项卡时,其他选项卡图标应该会更改,并且我想更改其他选项卡文本的颜色。请帮忙?

这是我的代码:

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

        }
    });

}

【问题讨论】:

  • 请添加您的代码

标签: java android view tabs android-tabhost


【解决方案1】:

尝试使用这个 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" />

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:
          // - 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
                  }
            } 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-07-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-11-25
        相关资源
        最近更新 更多