如何将ViewPager中的选项卡颜色从黑色更改为白色

时间:2014-04-29 10:49:47

标签: android android-actionbar android-viewpager

我想更改ViewPager中的标签颜色。我搜索但没有找到任何好的解决方案。我想将标签颜色从默认值更改为黑色到白色。他们以任何方式以编程方式执行此操作,因为我必须修改style.xml。我不知道怎么办,因为我是android新手。 PLZ有人帮忙

public class HomeActivity extends FragmentActivity implements ActionBar.TabListener 
    {

        public ViewPager viewPager;
        private PageAdapter mAdapter;
        private ActionBar actionBar;

        // Tab titles
        private String[] tabs = { "Calculate EMI", "EMI Schedule"};


        String TabFragmentSchedule;

         public void setTabFragmentSchedule(String t){
          TabFragmentSchedule = t;
         }

         public String getTabFragmentSchedule(){
          return TabFragmentSchedule;
         }


        @SuppressLint("NewApi")
        @Override
        protected void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_home);


            // Initilization
            viewPager = (ViewPager) findViewById(R.id.pager);
            actionBar = getActionBar();
            ColorDrawable colorDrawable = new ColorDrawable(Color.parseColor("#0F7BFF"));
            actionBar.setBackgroundDrawable(colorDrawable);

            mAdapter = new PageAdapter(getSupportFragmentManager());


            viewPager.setAdapter(mAdapter);
            actionBar.setHomeButtonEnabled(false);
            actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

            // Adding Tabs
            for (String tab_name : tabs) 
            {
                actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));
            }

            /**
             * on swiping the viewpager make respective tab selected
             * */
            viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

                @Override
                public void onPageSelected(int position) {
                    // on changing the page
                    // make respected tab selected
                    actionBar.setSelectedNavigationItem(position);

                }

                @Override
                public void onPageScrolled(int arg0, float arg1, int arg2) {
                }

                @Override
                public void onPageScrollStateChanged(int arg0) {
                }
            });



        }

        //method that is called to swipe viewpage on button click from calculation fragment
         public void switchToFragmentSchedule(){
            // viewPager.setCurrentItem(1);
             viewPager.setCurrentItem(1, false);
          }

        @Override
        public void onTabReselected(Tab tab, FragmentTransaction ft) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onTabSelected(Tab tab, FragmentTransaction ft) {
            // TODO Auto-generated method stub

            viewPager.setCurrentItem(tab.getPosition());

        }

        @Override
        public void onTabUnselected(Tab tab, FragmentTransaction ft) {
            // TODO Auto-generated method stub


        }

    }

1 个答案:

答案 0 :(得分:0)

您应该在xml文件中设置主题样式。您可以找到此任务的所有信息here

如果您需要以编程方式更改它,可以使用

mActionBar.setBackgroundDrawable(new ColorDrawable(0xffffffff)); mActionBar.setDisplayShowTitleEnabled(false); mActionBar.setDisplayShowTitleEnabled(true);

作为初学者,您可能还会发现this styling tool非常有帮助。