【问题标题】:Change action bar tab text colour更改操作栏选项卡文本颜色
【发布时间】:2014-01-16 16:43:18
【问题描述】:

我有一个使用滑动操作栏的应用程序。我有像 Tab1、Tab2、Tab3 这样的标题的标签。我想为每个标签标题设置不同的颜色。 Tab1(红色) Tab2(蓝色)(Tab3)绿色。我已经搜索并想出了使用 TabHost 的方法,但我不想使用它。有没有其他方法可以实现这一点。 这是主要活动

public class MainActivity extends FragmentActivity implements
        ActionBar.TabListener {

    SectionsPagerAdapter mSectionsPagerAdapter;
    ViewPager mViewPager;

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

        // Set up the action bar.
        final ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // Create the adapter that will return a fragment for each of the three
        // primary sections of the app.
        mSectionsPagerAdapter = new SectionsPagerAdapter(
                getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        // When swiping between different sections, select the corresponding
        // tab. We can also use ActionBar.Tab#select() to do this if we have
        // a reference to the Tab.
        mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                    @Override
                    public void onPageSelected(int position) {
                        actionBar.setSelectedNavigationItem(position);
                    }
                });


        Tab tab = actionBar.newTab().setIcon(R.drawable.prf).setText(getString(R.string.title_section1)).setTabListener(this);
        actionBar.addTab(tab, true);

        tab = actionBar.newTab().setIcon(R.drawable.pack).setText(getString(R.string.title_section2)).setIcon(R.drawable.pack).setTabListener(this);
        actionBar.addTab(tab);

        tab = actionBar.newTab().setIcon(R.drawable.call).setText(getString(R.string.title_section3)).setTabListener(this);
        actionBar.addTab(tab);

        tab = actionBar.newTab().setIcon(R.drawable.prom).setText(getString(R.string.title_section4)).setTabListener(this);
        actionBar.addTab(tab);

        tab = actionBar.newTab().setIcon(R.drawable.infoin).setText(getString(R.string.title_section5)).setTabListener(this);
        actionBar.addTab(tab);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onTabSelected(ActionBar.Tab tab,FragmentTransaction fragmentTransaction) {
        // When the given tab is selected, switch to the corresponding page in
        // the ViewPager.
        mViewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab,FragmentTransaction fragmentTransaction) {
    }

    @Override
    public void onTabReselected(ActionBar.Tab tab,FragmentTransaction fragmentTransaction) {
    }

    /**
     * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
     * one of the sections/tabs/pages.
     */
    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
            case 0 :
                return new Home();
            case 1:
                return new info();
            case 2:
                return new Plan();
            }
            return null;
        }

        @Override
        public int getCount() {
            // Show 3 total pages.
            return 3;
        }
    }


}

【问题讨论】:

    标签: android xml android-fragments android-viewpager


    【解决方案1】:

    您可以为每个选项卡设置自定义视图。为选项卡创建一个新的布局资源(它可以只是一个 TextView)。将其背景留空,并为选择指示器制作一个可绘制九个补丁的图形。使用

    获取 LayoutInflater
    LayoutInflater inflater = getSystemService(LAYOUT_INFLATER_SERVICE);
    

    然后对于每个选项卡,您可以这样做:

    Tab tab = ab.newTab()
        .setText("TY1")
        .setTabListener(new MyTabListener(this, TY1.class.getName()));
    View tabView = inflater.inflate(R.layout.my_tab_layout, null);
    tabView.setBackgroundColor(...); // set custom color
    tab.setCustomView(tabView);
    ab.addTab(tab);
    

    如果您想手动创建,check This Post,您的问题将得到解决。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-05
      • 2012-07-31
      相关资源
      最近更新 更多