2012-08-13 132 views
16

我在我的应用中使用应用主题Theme.Black。在这个主题中,动作栏是灰色的。我如何改变我的动作栏颜色?这是我的尝试:在android中更改动作栏颜色

<?xml version="1.0" encoding="utf-8"?> 
<resources xmlns:android="http://schemas.android.com/apk/res/android"> 

    <style name="mytheme" parent="@android:style/Theme.Black" > 

    </style> 
    <style name="Widget.MyApp.ActionBar" parent="@android:style/Widget.ActionBar"> 
     <item name="android:background">@android:color/black</item> 
    </style> 



</resources> 

但是,它不起作用。有任何想法吗?

回答

0

也许这也可以帮助你。这是从网站:

http://nathanael.hevenet.com/android-dev-changing-the-title-bar-background/

首先第一件事情你需要有申报为您的应用自定义主题(或活动,根据您的需要)。喜欢的东西...

<!-- Somewhere in AndroidManifest.xml --> 
<application ... android:theme="@style/ThemeSelector"> 

然后,声明你的自定义主题为两种情况,API版本,有和没有的全息主题。对于旧的主题,我们将自定义windowTitleBackgroundStyle属性,并为新的主题定制ActionBarStyle。

<!-- res/values/styles.xml --> 
<?xml version="1.0" encoding="utf-8"?> 
<resources> 

    <style name="ThemeSelector" parent="android:Theme.Light"> 
     <item name="android:windowTitleBackgroundStyle">@style/WindowTitleBackground</item> 
    </style> 

    <style name="WindowTitleBackground">  
     <item name="android:background">@color/title_background</item> 
    </style> 

</resources> 

<!-- res/values-v11/styles.xml --> 
<?xml version="1.0" encoding="utf-8"?> 
<resources> 

    <style name="ThemeSelector" parent="android:Theme.Holo.Light"> 
     <item name="android:actionBarStyle">@style/ActionBar</item> 
    </style> 

    <style name="ActionBar" parent="android:style/Widget.Holo.ActionBar">  
     <item name="android:background">@color/title_background</item> 
    </style> 

</resources> 

这就是它!

+0

'values-v14'文件夹怎么样? – Si8 2013-08-20 01:59:09

+0

你的意思是什么? – 2013-08-25 12:51:40

+0

对不起。我的意思是,如何使用Java而不是XML进行更改? – Si8 2013-08-25 13:37:42

18
ActionBar bar = getActionBar(); 
bar.setBackgroundDrawable(new ColorDrawable("COLOR")); 

它为我工作here

+1

谢谢。它为我工作。 getSupportActionBar()。setBackgroundDrawable(new ColorDrawable(getResources()。getColor(R.color.material_blue_gray1))); – Ataru 2015-04-26 08:54:00

+0

根据我的经验,设置它有一个缺点,即操作栏菜单颜色不会改变。 – Elye 2016-01-08 11:07:28

8
<style name="AppTheme" parent="AppBaseTheme"> 

    <item name="android:actionBarStyle">@style/MyActionBar</item> 
</style> 

<style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar"> 
    <item name="android:background">#C1000E</item> 
    <item name="android:titleTextStyle">@style/AppTheme.ActionBar.TitleTextStyle</item> 
</style> 

<style name="AppTheme.ActionBar.TitleTextStyle" parent="@android:style/TextAppearance.StatusBar.Title"> 
    <item name="android:textColor">#E5ED0E</item> 
</style> 

我已经解决了使用。

+3

我已经使用这个soved。 – zaptech 2014-05-01 09:56:51

7
ActionBar actionBar; 

actionBar = getActionBar(); 
ColorDrawable colorDrawable = new ColorDrawable(Color.parseColor("#93E9FA"));  
actionBar.setBackgroundDrawable(colorDrawable); 
+2

'新的ColorDrawable(0x93E9FA);' – 2015-05-12 19:36:53

0

如果您使用Android的默认操作栏,那么。如果你从java改变,那么有时会显示以前的颜色。

enter image description here

内。然后 “app_bar_main” 你的行动条码。因此,进入app_bar_main.xml并添加Background。

<?xml version="1.0" encoding="utf-8"?> 

<android.support.design.widget.AppBarLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:theme="@style/AppTheme.AppBarOverlay"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="#33691e" <!--use your color --> 
     app:popupTheme="@style/AppTheme.PopupOverlay" /> 

</android.support.design.widget.AppBarLayout> 
<include layout="@layout/content_main1" /> 

0

,如果你在你的布局activity_main

<android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:popupTheme="@style/AppTheme.PopupOverlay" /> 

,你必须把

在活动验证码

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

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 
    toolbar.setBackgroundColor(Color.CYAN); 
} 
0

只需简单地去RES /价值/风格。xml文件和编辑XML文件改变xml文件。这里的颜色是示例代码

<resources> 

<!-- Base application theme. --> 
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 
    <!-- Customize your theme here. --> 

//以下代码是用于改变操作栏的颜色

<item name="colorPrimary">"type your color code here. eg:#ffffff"</item> 
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
    <item name="colorAccent">@color/colorAccent</item> 
</style> 

<style name="AppTheme.NoActionBar"> 
    <item name="windowActionBar">false</item> 
    <item name="windowNoTitle">true</item> 
</style> 

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" /> 

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" /> 

希望它能帮助你...