代码之家  ›  专栏  ›  技术社区  ›  ajay dhadhal

更改自定义选项卡布局文本视图背景色

  •  0
  • ajay dhadhal  · 技术社区  · 6 年前

    我想更改自定义选项卡的背景色。我在自定义选项卡中有文本视图。我尝试了以下代码,但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 回复  |  直到 6 年前
        1
  •  2
  •   Mohammad Adil    6 年前

    您可以通过使用xml文件来实现这一点。只需将此添加到您的视图中。。。

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

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

    tab\u selector\u注册表 文件

    <?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>
    
        2
  •  2
  •   Mahesh Keshvala    6 年前

    使用以下代码进行选择:

    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) {
        }
    });
    
        3
  •  1
  •   ajay dhadhal    6 年前

    我犯了一个小错误。右图如下。

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