Android Changing Tab Text Color

这一生的挚爱 提交于 2019-12-25 16:57:10

问题


I am trying to set Android tab text color by doing this in my style.xml:

<resources xmlns:android="http://schemas.android.com/apk/res/android">

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

<style name="ActionBarStyle" parent="android:style/Widget.Holo.Light.ActionBar">
    <item name="android:background">#FFFFFF</item>
    <item name="android:titleTextStyle">@style/myTheme.ActionBar.Text</item>
    <item name="android:textStyle">bold</item>
    <item name="android:textSize">16sp</item>
</style>

<style name="myTheme.ActionBar.Text" parent="@android:style/TextAppearance">
    <item name="android:textColor">#000000</item>
</style>

And my View pager as such:

<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" 
android:textColor="#000000"/>

However, with these codes, I only managed to change the view pager tab background color to white but text as white color as well. I wonder why the part where I set text color for action bar does not work.

Thanks in advance.

EDIT

And the part where I set my view pager:

public class EventDetailMain extends FragmentActivity {
Context context = this;

ViewPager Tab;
EventDtlTabPagerAdapter TabAdapter;
ActionBar actionBar;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.event_detail_main);
    TabAdapter = new EventDtlTabPagerAdapter(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);
    actionBar = getActionBar();
    // Enable Tabs on Action Bar
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    ActionBar.TabListener tabListener = new ActionBar.TabListener() {
        public void onTabReselected(android.app.ActionBar.Tab tab,
                android.app.FragmentTransaction ft) {
        }

        public void onTabSelected(android.app.ActionBar.Tab tab,
                android.app.FragmentTransaction ft) {
            // on tab selected show respected fragment view
            Tab.setCurrentItem(tab.getPosition());
        }

        public void onTabUnselected(android.app.ActionBar.Tab tab,
                android.app.FragmentTransaction ft) {
        }
    };

    // Add New Tab
    actionBar.addTab(actionBar.newTab().setText("Event Detail")
            .setTabListener(tabListener));
    actionBar.addTab(actionBar.newTab().setText("Chat Room")
            .setTabListener(tabListener));
    actionBar.addTab(actionBar.newTab().setText("Drop Review")
            .setTabListener(tabListener));
}

回答1:


I believe there is a style for the Tab's text try out this:

add this your theme

<item name="android:actionBarTabTextStyle">@style/ActionBar.TabText</item>

and then declare the tab text style as following

<style name="ActionBar.TabText" parent="">
        <item name="android:textColor">#888</item>
    </style>



回答2:


Try this by changing the parent of the style to to the title of the actionbar for the theme:

<style name="myTheme.ActionBar.Text" parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Title">
    <item name="android:textColor">#000000</item>
</style>



回答3:


OK Try look at docs (http://developer.android.com/guide/topics/ui/actionbar.html):

<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme"
       parent="@style/Theme.AppCompat.Light">
    <item name="android:actionBarStyle">@style/MyActionBar</item>
<!--IMPORTANT FOR YOU -->
    <item name="android:actionBarTabTextStyle">@style/TabTextStyle</item> 

    <item name="android:actionMenuTextColor">@color/actionbar_text</item>

    <!-- Support library compatibility -->
    <item name="actionBarStyle">@style/MyActionBar</item>
    <item name="actionBarTabTextStyle">@style/TabTextStyle</item>
    <item name="actionMenuTextColor">@color/actionbar_text</item>
</style>

<!-- general styles for the action bar -->
<style name="MyActionBar"
       parent="@style/Widget.AppCompat.ActionBar">
    <item name="android:titleTextStyle">@style/TitleTextStyle</item>
    <item name="android:background">@drawable/actionbar_background</item>
    <item name="android:backgroundStacked">@drawable/actionbar_background</item>
    <item name="android:backgroundSplit">@drawable/actionbar_background</item>

    <!-- Support library compatibility -->
    <item name="titleTextStyle">@style/TitleTextStyle</item>
    <item name="background">@drawable/actionbar_background</item>
    <item name="backgroundStacked">@drawable/actionbar_background</item>
    <item name="backgroundSplit">@drawable/actionbar_background</item>
</style>

<!-- action bar title text  -->
<style name="TitleTextStyle"
       parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
    <item name="android:textColor">@color/actionbar_text</item>
</style>

<!-- action bar tab text IMPORTANT FOR YOU -->
<style name="TabTextStyle"
       parent="@style/Widget.AppCompat.ActionBar.TabText">
    <item name="android:textColor">@color/actionbar_text</item>
</style>




回答4:


Let's say you have a custom theme:

<style name="TabTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="android:textColorPrimary">@color/colorPrimaryDark</item>
    <item name="android:textColorSecondary">@color/colorPrimaryDark</item>
</style>

Please note that

parent="Theme.AppCompat.Light.DarkActionBar"

can be any other theme you already have, as long as it inherits from a standard android theme. I just used DarkActionBar as an example.

The android:textColorPrimary attribute will serve as the selected tab's text color, whereas the android:textColorSecondary will be used as the unselected tab's text color.

I hope that worked for you as it did for me.



来源:https://stackoverflow.com/questions/26910091/android-changing-tab-text-color

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!