在android中更改操作栏颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11930587/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:12:13  来源:igfitidea点击:

Change action bar color in android

androidandroid-layout

提问by user1302569

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

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

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

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

回答by user3225831

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

it worked for me here

它在这里对我 有用

回答by zaptech

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

我已经解决了使用那个。

回答by Steve

 ActionBar actionBar;

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

回答by Gokul Sreenivasan

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

只需简单地转到 res/values/styles.xml 文件并编辑 xml 文件以更改 xml 文件的颜色。这是示例代码

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

希望它会帮助你...

回答by MorZa

Updated code:

更新代码:

getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.your_color)));

回答by Anamika Singh

onclick buttons c2to fetch and change the color of action bar and notification bar according to button's background, hope this helps

onclick 按钮c2根据按钮的背景获取和更改操作栏和通知栏的颜色,希望这会有所帮助

int color = c2.getBackground()).getColor();
int colorlighter = -color * 40 / 100 + color;
getWindow().setNavigationBarColor(colorlighter);
getWindow().setStatusBarColor(colorlighter);
ActionBar bar = getSupportActionBar();
bar.setBackgroundDrawable(new ColorDrawable(color));

colorlighteris used to set color of notification bar a little lighter than action bar
coloris the background color of button c2, according to which we change our color.

colorlighter用于设置通知栏的颜色比操作栏浅一点
color是按钮的背景颜色c2,我们根据它更改颜色。

回答by Bruno Bieri

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

也许这也可以帮助你。它来自网站:

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

http://nathanael.hevenet.com/android-dev-changed-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.

然后,为两种情况声明您的自定义主题,带和不带 Holo 主题的 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>

That's it!

就是这样!

回答by Md Imran Choudhury

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

如果您使用 Android 默认操作栏,则。如果您从 java 更改,则一段时间显示以前的颜色。

enter image description here

在此处输入图片说明

Example

例子

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

然后是“app_bar_main”中的操作条码。所以进入 app_bar_main.xml 并添加背景。

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

回答by Cabezas

if you have in your layout activity_main

如果你的布局中有 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);
}