如何使用viewpager更改android中的选项卡的文本颜色和android中的选项卡的背景

时间:2016-12-27 05:28:23

标签: android android-viewpager android-tabhost

我是android的新手,并使用viewpager准备了可跳转选项卡的演示,并已成功准备,现在我想更改选项卡的文本颜色以及选择,请帮我解决, 的 style.xml

    <style name="MyRadioButtonStyle" parent="@android:style/Widget.CompoundButton.RadioButton">
        <item name="android:button">@drawable/radio_selected</item>
    </style>

    <style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
        <item name="colorPrimary">@color/orange</item>
        <item name="colorPrimaryDark">@color/orange</item>
        <item name="colorAccent">@color/orange</item>

        <item name="android:actionBarStyle">@style/MyActionBarTheme</item>
    </style>
    <style name="MyActionBarTheme" parent="@android:style/Widget.Holo.Light.ActionBar">
        <item name="android:background">@color/orange</item>
        <item name="android:textColor">@color/white</item>
    </style>

    <style name="TabSpecialTheme" parent="android:Theme.Holo.Light.DarkActionBar">
        <item name="@android:attr/actionBarTabTextStyle">@style/TabStyle</item>
    </style>

    <!-- Modify the text color -->
    <style name="TabStyle" parent="android:Widget.Holo.Light.ActionBar.TabText.Inverse">
        <item name="android:textColor">#F70000</item>
    </style>


    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>


</resources>

main_listing.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    />

mainListing.java

public class MainListingActivity extends FragmentActivity {
    ViewPager Tab;
    TabsPagerAdapter TabAdapter;
    ActionBar actionBar;

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

        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);
                        actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#F05423")));

                    }
                });
        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

            }};
        //Add New Tab
        actionBar.addTab(actionBar.newTab().setText("All").setTabListener(tabListener));
        actionBar.addTab(actionBar.newTab().setText("Gents").setTabListener(tabListener));
        actionBar.addTab(actionBar.newTab().setText("Ladies").setTabListener(tabListener));
        actionBar.addTab(actionBar.newTab().setText("Kids").setTabListener(tabListener));


    }

    //change tab color..
    public void setTabColor(TabHost tabhost) {

        for (int i = 0; i < tabhost.getTabWidget().getChildCount(); i++)
            tabhost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#ffffff")); //unselected

        if (tabhost.getCurrentTab() == 0) {
            tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab()).setBackgroundColor(Color.parseColor("#F05423"));
            //1st tab selected
        } else {
            tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab()).setBackgroundColor(Color.parseColor("#F05423"));
        }
    }

}

1 个答案:

答案 0 :(得分:0)

   
<style name="TabSpecialTheme" parent="android:Theme.Holo.Light.DarkActionBar">
  <item name="@android:attr/actionBarTabTextStyle">@style/TabStyle</item>
</style>

<!-- Modify the text color -->
<style name="TabStyle" parent="android:Widget.Holo.Light.ActionBar.TabText.Inverse">
  <item name="android:textColor">#F70000</item>
</style>>