如何在android

时间:2015-10-03 11:56:23

标签: android

我是android新手,如何更改我选择的标签文字颜色 我的主要活动

public class MainActivity extends FragmentActivity implements
    ActionBar.TabListener {

private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;

// Tab titles
private String[] tabs = { "MEN-Clothing", "WOMEN", "KIDS" };

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

    // Initilization
    viewPager = (ViewPager) findViewById(R.id.pager);
    actionBar = getActionBar();
    mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

    viewPager.setAdapter(mAdapter);
    //actionBar.setHomeButtonEnabled(false);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);        
   // getActionBar().setDisplayUseLogoEnabled(false);
    getActionBar().setDisplayShowHomeEnabled(false);



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




@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
    // on tab selected
    // show respected fragment view
    viewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}

} 如何更改我选择的标签文本颜色在android任何一个帮助我,我的当前文字颜色是灰码颜色,当我选择任何标签我的文字颜色应该是白色

这里是我的style.xml代码

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

<!--
    Base application theme, dependent on API level. This theme is replaced
    by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light">

    <!--
        Theme customizations available in newer API levels can go in
        res/values-vXX/styles.xml, while customizations related to
        backward-compatibility can go here.
    -->
</style>




<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <item name="android:actionBarStyle">@style/MyActionBarTheme</item>
    <item name="android:actionBarTabTextStyle">@style/ActionBarTabText</item>
    <item name="android:titleTextStyle">@style/MyActionBarTitleText</item>
  <item name="android:actionBarTabStyle">@style/tabStyle</item>

<item name="android:actionBarDivider">@null</item>
    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
 <style name="MyActionBarTheme" parent="@android:style/Widget.Holo.Light.ActionBar">
    <item name="android:background">#029C7A</item>

</style>
<style name="ActionBarTabText" parent="@android:style/Widget.Holo.Light.ActionBar">       
<item name="android:textColor">#CFCFC4</item>
<item name="android:gravity">center</item>
</style> 

        #FFFFFF         

       假         #029C7A     

2 个答案:

答案 0 :(得分:0)

您可以在MainActivity中调用这些覆盖方法并在那里更改文本

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

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
    // TODO Auto-generated method stub
    // on tab selected
    // show respected fragment view
    viewPager.setCurrentItem(tab.getPosition());
tab.setTabTextColors(ContextCompat.getColor(mContext, R.color.white);

}

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

}

希望这可能有助于你的事业, 如果没有给我留言。

答案 1 :(得分:0)

你可以

@覆盖 public void onTabSelected(Tab tab,FragmentTransaction ft){

//在选项卡上

viewPager.setCurrentItem(tab.getPosition());

//在这里您可以处理选项卡

}