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

时间:2018-04-24 09:45:10

标签: android textview android-tablayout

我想更改自定义标签的背景颜色。我在自定义标签中有Textview。我试过以下代码但Textveiw背景颜色没有改变。

    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);               
            }
        }
    });

先谢谢

3 个答案:

答案 0 :(得分:2)

你可以使用xml文件来实现。只需在你的视图中添加它......

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>

答案 1 :(得分:2)

使用以下代码进行选择:

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

答案 2 :(得分:1)

我有一个小Mistack。正确的是。

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);                
            }
        }
    });