Add a different color for each action bar tabs separately

后端 未结 2 1670
南笙
南笙 2021-01-29 00:06

I need to add a different color for every tabs.

For Eg: like this below image

\"enter

2条回答
  •  抹茶落季
    2021-01-29 00:34

    yes,finally I done it.

    MainActivity.java:

    public class MainActivity extends FragmentActivity {
        static ViewPager Tab;
        TabsPagerAdapter TabAdapter;
        ActionBar actionBar;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            TabAdapter = new TabsPagerAdapter(getSupportFragmentManager());
    
            Tab = (ViewPager) findViewById(R.id.pager);
    
            Tab.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                @Override
                public void onPageSelected(int position) {
                    actionBar = getActionBar();
                    actionBar.setSelectedNavigationItem(position);
                }
            });
    
            Tab.setAdapter(TabAdapter);
            actionBar = getActionBar();
            // Enable Tabs on Action Bar
            actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
            ActionBar.TabListener tabListener = new ActionBar.TabListener() {
                @Override
                public void onTabReselected(android.app.ActionBar.Tab tab,
                        FragmentTransaction ft) {
                    // TODO Auto-generated method stub
                }
    
                @Override
                public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
                    Tab.setCurrentItem(tab.getPosition());
                }
    
                @Override
                public void onTabUnselected(android.app.ActionBar.Tab tab,
                        FragmentTransaction ft) {
                    // TODO Auto-generated method stub
                }
            };
    
            LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
    
            ActionBar.Tab tab = actionBar.newTab().setText("Home")
                    .setTabListener(new TabListener(this, Home.class.getName()));
            View tabView = inflater.inflate(R.layout.fragment_home, null);
            tabView.setBackgroundResource(R.drawable.gradient_shape); // set custom
                                                                        // color
    
            tab.setCustomView(tabView);
            actionBar.addTab(tab);
    
            tab = actionBar.newTab().setText("News")
                    .setTabListener(new TabListener(this, News.class.getName()));
            View tabView2 = inflater.inflate(R.layout.fragment_news, null);
            tabView2.setBackgroundResource(R.drawable.gradient_shape2); // set
                                                                        // custom
                                                                        // color
            tab.setCustomView(tabView2);
            actionBar.addTab(tab);
    
            tab = actionBar.newTab().setText("Latest")
                    .setTabListener(new TabListener(this, Latest.class.getName()));
            View tabView3 = inflater.inflate(R.layout.fragment_latest, null);
            tabView3.setBackgroundResource(R.drawable.gradient_shape3); // set
                                                                        // custom
                                                                        // color
            tab.setCustomView(tabView3);
            actionBar.addTab(tab);
    
        }
    
        public static class TabListener extends Fragment implements
                ActionBar.TabListener {
    
            public TabListener(MainActivity mainActivity, String name) {
                // this(mainActivity,name);
            }
    
            @Override
            public void onTabSelected(android.app.ActionBar.Tab tab,
                    FragmentTransaction ft) {
                Tab.setCurrentItem(tab.getPosition());
            }
    
            @Override
            public void onTabUnselected(android.app.ActionBar.Tab tab,
                    FragmentTransaction ft) {
    
            }
    
            @Override
            public void onTabReselected(android.app.ActionBar.Tab tab,
                    FragmentTransaction ft) {
    
            }
    
        }
    
    }
    

    gradient_shape.xml:

    
    
    
        
    
        
    
        
    
        
    
    
    

    Output:

    enter image description here

    Hope it will be helpful.

提交回复
热议问题