简体   繁体   中英

How to change tab indicator color and text color while swiping tab using ViewPager?

This is my code. How to add tabindicator color and text color in this java class?

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

        actionBar  = getActionBar();
        actionBar.setDisplayShowHomeEnabled(false);  // hides action bar icon
        actionBar.setDisplayShowTitleEnabled(false); // hides action bar title

        LayoutInflater mInflater = LayoutInflater.from(this);

     mCustomView = mInflater.inflate(R.layout.custom_menu, null);

        i = (ImageButton)mCustomView.findViewById(R.id.imageButton);

        actionBar.setDisplayShowCustomEnabled(true);

    e = (EditText)mCustomView. findViewById(R.id.editText1);
    e.setVisibility(mCustomView.INVISIBLE);



        TabAdapter = new SectionsPagerAdapter(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);
     //   Tab.showContextMenu();
        actionBar = getActionBar();
        //Enable Tabs on Action Bar
      actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

                // set background for action bar tab
        actionBar.setStackedBackgroundDrawable(new ColorDrawable(Color.parseColor("#FFFFFF")));       

        actionBar.show();

        ActionBar.TabListener tabListener = new ActionBar.TabListener(){

            @Override
            public void onTabReselected(android.app.ActionBar.Tab tab,
                    FragmentTransaction ft) {

            }

            @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

            }};
            //Add New Tab
            actionBar.addTab(actionBar.newTab().setText("SPOTLIGHT").setTabListener(tabListener));
            actionBar.addTab(actionBar.newTab().setText("WHAT'S NEW").setTabListener(tabListener));
            actionBar.addTab(actionBar.newTab().setText("HOT ITEMS").setTabListener(tabListener));
    }
}

You can create a tab layout where each tab has a custom textView and add that tab in the tablayout with whatever color of your choice

View customView = LayoutInflater.from(getContext()).inflate(R.layout.tab_text, null); 

addTab(newTab().setCustomView(customView));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM