2014-01-26 93 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這個答案嗎? –

相關問題