2014-01-26 45 views
4

我试图以编程方式更改操作栏选项卡的颜色。我默认在styles.xml中将它们设置为红色,这就是我希望它们与viewpager中的其他选项卡相同的方式,但是,在第一个选项卡上,我希望操作栏和导航选项卡都变为透明。我已经通过使用此代码与动作栏做到了这一点以编程方式更改导航选项卡的颜色

@Override 
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { 
    // When the given tab is selected, switch to the corresponding page in 
    // the ViewPager. 
    int newActionBarColor/*, newActionBarTabColor*/; 
    if(tab.getPosition() == 0) { 
     newActionBarColor = Color.parseColor("#" + Integer.toHexString(getResources().getColor(R.color.actionBarTransparent))); 
     //newActionBarTabColor = Color.parseColor("#" + Integer.toHexString(getResources().getColor(R.color.actionBarTabsTransparent))); 
    } else { 
     newActionBarColor = Color.parseColor("#" + Integer.toHexString(getResources().getColor(R.color.actionBar))); 
     //newActionBarTabColor = Color.parseColor("#" + Integer.toHexString(getResources().getColor(R.color.actionBarTabs))); 
    } 
    getSupportActionBar().setBackgroundDrawable(new ColorDrawable(newActionBarColor)); 
    //getSupportActionBar().setStackedBackgroundDrawable(new ColorDrawable(newActionBarTabColor)); 
    //getSupportActionBar().setSplitBackgroundDrawable(new ColorDrawable(newActionBarTabColor)); 
    mViewPager.setCurrentItem(tab.getPosition()); 
} 

但我不能得到这与标签,任何想法的工作?你可以看到我上面试过的东西。

活动的标签,我想有色标签:

目前,我有,我想无论是动作栏和标签为透明标签上什么:

回答

0

仅当支持Android 3.0及更高版本时,您可以像这样定义操作栏的背景:

res/values/themes.xml 
<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <!-- the theme applied to the application or activity --> 
    <style name="CustomActionBarTheme" 
      parent="@android:style/Theme.Holo.Light.DarkActionBar"> 
     <item name="android:actionBarStyle">@style/MyActionBar</item> 
    </style> 

    <!-- ActionBar styles --> 
    <style name="MyActionBar" 
      parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse"> 
     <item name="android:background">@drawable/actionbar_background</item> 
    </style> 
</resources> 

你的主题,然后应用到您的整个应用程序或个别活动:

<application android:theme="@style/CustomActionBarTheme" ... /> 

当使用支持库,相同主题的上述而是必须是这样的:

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

     <!-- Support library compatibility --> 
     <item name="actionBarStyle">@style/MyActionBar</item> 
    </style> 

    <!-- ActionBar styles --> 
    <style name="MyActionBar" 
      parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse"> 
     <item name="android:background">@drawable/actionbar_background</item> 

     <!-- Support library compatibility --> 
     <item name="background">@drawable/actionbar_background</item> 
    </style> 
</resources> 

然后申请您的主题,以您的整个应用程序或个人活动:

<application android:theme="@style/CustomActionBarTheme" ... /> 

您可以从以下链接中获得更多信息:Styling the Action Bar

+0

这不会改变任何内容,我不知道方式。我将其完全复制到它,并将它保留为默认颜色,当它将它设置为红色时。 –

+0

在此期间,我找到了以下链接可以帮助你:'http:// jgilfelt.github.io/android-actionbarstylegenerator /' –

+0

如果它对你有帮助,你可以使用upvote这个答案吗? –

相关问题