如何更改选择选项卡(活动或非活动选项卡)背景颜色和文本颜色

时间:2015-11-27 12:25:24

标签: android tabs

请找到以下代码:

  // this is on create method for creating view  


@Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                System.out.println("DirectoryScreen.onCreate()");
                HINT_PREFIX =  getString(R.string.search_by) + " ";

                requestWindowFeature(Window.FEATURE_NO_TITLE);

                setContentView(R.layout.screen_tabs_directory);
        // getting data from intent
                String mTitle = getIntent().getStringExtra(Keys.EXTRA_BANNER_TITLE);
                mTitle = mTitle == null ? getString(R.string.directory) : mTitle;
                if (isSideBarLayout()) {
                    doSideBarHeader(mTitle, DirectoryScreen.this);
                } else {
                    doStandardHeader(mTitle, getString(R.string.home), DirectoryScreen.this);
                }

// this is for activate action bar
                activateActionBar(false,false,true);   

                Cache.ADVISOR_LABEL = getString(R.string.directory_advisor_label);
                Cache.ADVISOR_PRIMARY_LABEL = getString(R.string.directory_primary_advisor_label);
                isPrivateModule = getIntent().getBooleanExtra(Keys.MODULE_IS_PRIVATE, false);

                Dictionary d = Modules.DIRECTORY.getVersionDictionary();
                Cache.DIRECTORY_BASE_URL = d.getValue("url") + "/audience/";

                if ( Cache.DIRECTORY_DICTIONARY == null )
                    uploadDirectoryDictionary();

                if ( Cache.DIRECTORY_DICTIONARY != null ) 
                    isPrivateModule = "true".equals(Cache.DIRECTORY_DICTIONARY.getValue("private"));            

                String savedSearchText = Persistence.getInstance().getString(Keys.DIRECTORY_SEARCH_TEXT);
                String savedChoice = Persistence.getInstance().getString(Keys.DIRECTORY_SEARCH_SCOPING_FIELD);


                Dictionary[] fields = d.getChildArray("scopingFields");
                if(fields == null) {
                    fields = d.getChildArray("Items");
                    if(fields == null) {
                        findViewById(R.id.header_optional_view_id).setVisibility(View.GONE);
                    }
                }
                fields = null == fields ? new Dictionary[0] : fields;

                for (Dictionary field : fields) {
                    String s = field.getValue("displayString");
                    Cache.DIRECTORY_KEY_MAP.put(s, field.getValue("key"));
                }

                if ( Cache.SELECTED_DIRECTORY_OPTION == null ) {
                    Cache.SELECTED_DIRECTORY_OPTION = fields.length > 0 ? fields[0].getValue("displayString") : "";
                }

                final int oveflowColor = Style.getActionbarOverflowColor(this);
                ColorFilter filter = new LightingColorFilter( oveflowColor, 0x000000 );
                ImageView mFilterView = (ImageView) findViewById(R.id.header_optional_view_id);
                Drawable filterIcon = getResources().getDrawable( R.drawable.ic_action_filter ); 
                filterIcon.setColorFilter(filter);
                mFilterView.setImageDrawable(filterIcon); 
                if ( fields.length > 1 )
                    mFilterView.setVisibility(View.VISIBLE);
                else
                    mFilterView.setVisibility(View.GONE);
                mFilterView.setOnClickListener(DirectoryScreen.this);

                ImageView mSearchCancelView = (ImageView) findViewById(R.id.directory_cancel_search_button);
                Drawable cancelIcon = getResources().getDrawable( R.drawable.ic_action_clear ); 
                cancelIcon.setColorFilter(filter);
                mSearchCancelView.setImageDrawable(cancelIcon);  
                mSearchCancelView.setOnClickListener(DirectoryScreen.this);     

                mSearchView = (EditText) findViewById(R.id.directory_search_id);
                mSearchView.setTextColor(Style.getTextBannerColor(this));
                mSearchView.setHint(fields.length > 0 ? HINT_PREFIX + Cache.SELECTED_DIRECTORY_OPTION : HINT_PREFIX + getString(R.string.name_by));     
                mSearchView.setOnEditorActionListener(this);
                if (!U.isEmpty(savedSearchText)) {
                    mSearchView.setText(savedSearchText);
                }       


                mPagerAdapter = new MyPagerAdapter(getSupportFragmentManager());

                mViewPager = (ViewPager) findViewById(R.id.pager);
                mViewPager.setAdapter(mPagerAdapter);

                mSlidingTabLayout = (SlidingTabLayout) findViewById(R.id.sliding_tabs);
                mSlidingTabLayout.setTextColor(oveflowColor);
                mSlidingTabLayout.setDistributeEvenly(true);
                mSlidingTabLayout.setBackgroundDrawable(getBackground());
                // setting tab indicator color
                mSlidingTabLayout.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
                    @Override
                    public int getIndicatorColor(int position) {

                        return oveflowColor;
                    }
                });

                // this is for on page change listener
                mSlidingTabLayout.setOnPageChangeListener(new OnPageChangeListener() {

                    @Override
                    public void onPageSelected(int arg0) {
                        // TODO Auto-generated method stub              
                    }

                    @Override
                    public void onPageScrolled(int position, float arg1, int arg2) {
                        // TODO Auto-generated method stub


                        int activeColor = Style.getTextButtonNormalColor(DirectoryScreen.this);
                        int disableColor = Style.getTextButtonFocusColor(DirectoryScreen.this);
                         for(int i=0; i < mViewPager.getChildCount(); i++){
                             System.out.println("index of view in viewpager --->"+i);
                                View tv = (View) mViewPager.getChildAt(i);
                                if(i == position){
                                       //tv.setTextColor(Color.WHITE);
                                    tv.setBackgroundColor(activeColor);
                                } else {
                                    //tv.setTextColor(Color.GREY);
                                    tv.setBackgroundColor(disableColor);
                                }
                            }               

                    }

                    @Override
                    public void onPageScrollStateChanged(int arg0) {
                        // TODO Auto-generated method stub

                    }
                });


                mSlidingTabLayout.setViewPager(mViewPager);
                mSearchView.setBackgroundColor(Color.WHITE);


            }
    // this is my viewpager adapter for creating tabs on fragment
    public class MyPagerAdapter extends FragmentPagerAdapter {

            private LinkedHashMap<String, Fragment> mPagerFragmentMap; 
            private final String[] mFragmentViews = { getString(R.string.directory_all_contacts) , getString(R.string.directory_my_contacts) };
    // set fragment to my page
            public MyPagerAdapter(FragmentManager fm) {
                super(fm);
                mPagerFragmentMap = new LinkedHashMap<String, Fragment>();
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return mFragmentViews[position];
            }

            @Override
            public int getCount() {
                return mFragmentViews.length;
            }

            @Override
            public Fragment getItem (int position) {
                System.out.println("DirectoryScreen.MyPagerAdapter.getItem() position is-->"+ position);
                Fragment rc = null;
                String s = ((EditText) findViewById(R.id.directory_search_id)).getText().toString();
                Bundle args = null;
                switch (position)
                {
                case 0:             
                    if ( mPagerFragmentMap.containsKey(mFragmentViews[0])) {
                        System.out.println("case 0 if");
                        rc = mPagerFragmentMap.get(mFragmentViews[0]);
                    }
                    else {
                        System.out.println("case 0 else");
                        DirectoryFragment df = new DirectoryFragment();
                        args = new Bundle();
                        args.putInt("position", position);
                        args.putString("query", s);
                        df.setArguments(args);
                        rc = df;
                        mPagerFragmentMap.put(mFragmentViews[0], df);
                    }
                    break;
                case 1: 
                    if ( mPagerFragmentMap.containsKey(mFragmentViews[1])) {
                        System.out.println("case 1 if");
                        rc = mPagerFragmentMap.get(mFragmentViews[1]);
                    }
                    else {
                        System.out.println("case 1 else");
                        MyContactsDirectoryFragment cf = new MyContactsDirectoryFragment();
                        args = new Bundle();
                        args.putInt("position", position);
                        args.putString("query", s);
                        cf.setArguments(args);
                        rc = cf;
                        mPagerFragmentMap.put(mFragmentViews[1], cf);
                        break;
                    }
                }
                return rc;
            }
        }

enter image description here

1 个答案:

答案 0 :(得分:0)

使用这些代码行......`

  ALL_CONTACTS_VIEW.setOnClickListener(new View.OnClickListener() {
       @Override
     public void onClick(View v) {

       ALL_CONTACTS_VIEW.setBackgroundResource(R.drawable.ALL_CONTACTS_VIEW_ACTIVE);
       MY_CONTACT_VIEW.setBackgroundResource(R.drawable.MY_CONTACT_VIEW_INACTIVE);

      }
    });

  MY_CONTACT_VIEW.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

       MY_CONTACT_VIEW.setBackgroundResource(R.drawable.MY_CONTACT_VIEW_ACTIVE);
       ALL_CONTACTS_VIEW.setBackgroundResource(R.drawable.ALL_CONTACTS_VIEW_IN_ACTIVE);

      }
    });