Change action bar color in android

匿名 (未验证) 提交于 2019-12-03 02:14:01

问题:

I am using the app theme Theme.Black in my app. In this theme the action bar is gray. How I can change color of my action bar? This is my attempt:

<?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> 

However, it doesn't work. Any ideas?

回答1:

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

it worked for me here



回答2:

<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> 

I have Solved Using That.



回答3:

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


回答4:

Maybe this can help you also. It's from the website:

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

First things first you need to have a custom theme declared for your application (or activity, depending on your needs). Something like…

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

Then, declare your custom theme for two cases, API versions with and without the Holo Themes. For the old themes we’ll customize the windowTitleBackgroundStyle attribute, and for the newer ones the 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> 

That’s it!



回答5:

If you use Android default action bar then. If you change from java then some time show previous color.

Example

Then your action bar code inside "app_bar_main". So go inside app_bar_main.xml and just add Background.

Example

<?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" /> 



回答6:

if you have in your layout 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" /> 

you have to put

in your Activity this code

@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); } 


回答7:

Just simply go to res/values/styles.xml file and edit the xml file to change the color of xml file .Here is the sample code

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

// below code is for changing the color of action bar

    <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" /> 

Hope it will help you...



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