更改自定义选项卡布局 Textview 背景颜色

change Custom Tab layout Textview background color

我想更改自定义标签的背景颜色。我在自定义中有 Textview Tab.I 尝试了以下代码但是 Textview 背景颜色没有改变。

    final TextView tabText_customtab1=(TextView)findViewById(R.id.tabText_customtab1);
    final TextView tabText_customtab2=(TextView)findViewById(R.id.tabText_customtab2);   

    mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));

    mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener()
    {
        @Override
        public void onTabSelected(TabLayout.Tab tab)
        {
            if(tab.getPosition()==0)
            {
                tabText_customtab1.setBackgroundColor(getResources().getColor(R.color.selected_tab_color));
            }
            else if(tab.getPosition()==1)
            {
                tabText_customtab2.setBackgroundColor(getResources().getColor(R.color.unselected_tab_color));                  
            }
        }

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

        @Override
        public void onTabReselected(TabLayout.Tab tab)
        {
            if(tab.getPosition()==0)
            {
                tabText_customtab1.setBackgroundResource(R.color.selected_tab_color);
            }
            else if(tab.getPosition()==1)
            {
                tabText_customtab2.setBackgroundResource(R.color.unselected_tab_color);               
            }
        }
    });

提前致谢

me.The右手有小错

mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener()
    {
        @Override
        public void onTabSelected(TabLayout.Tab tab)
        {
            if(tab.getPosition()==0)
            {
                tabText_customtab1.setBackgroundResource(R.color.selected_tab_color);
                tabText_customtab2.setBackgroundResource(R.color.unselected_tab_color);
            }
            else if(tab.getPosition()==1)
            {
                tabText_customtab1.setBackgroundResource(R.color.unselected_tab_color);
                tabText_customtab2.setBackgroundResource(R.color.selected_tab_color);

            }
        }

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

        @Override
        public void onTabReselected(TabLayout.Tab tab)
        {
            if(tab.getPosition()==0)
            {
                tabText_customtab1.setBackgroundResource(R.color.selected_tab_color);
                tabText_customtab2.setBackgroundResource(R.color.unselected_tab_color);
            }
            else if(tab.getPosition()==1)
            {
                tabText_customtab1.setBackgroundResource(R.color.unselected_tab_color);
                tabText_customtab2.setBackgroundResource(R.color.selected_tab_color);                
            }
        }
    });

您可以使用您的 xml file.just 将其添加到您的视图中...

app:tabBackground="@drawable/tab_selector_reg" //for tab layout

或者您可以使用 android:background=""

tab_selector_reg 文件

<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/tab_selected" android:state_selected="true"/>
<item android:drawable="@color/water_app_yellow_transparent"/> </selector>

使用下面的代码进行选择:

mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        for (int i = 0; i < tabLayout.getTabCount(); i++) {
            if (i == position) {
                tabLayout.getTabAt(i).getCustomView().setBackgroundColor(getResources().getColor(R.color.selected_tab_color));
            } else {
                tabLayout.getTabAt(i).getCustomView().setBackgroundColor(getResources().getColor(R.color.unselected_tab_color));
            }
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {
    }
});