在安卓系统中改变操作栏颜色

19

我在我的应用程序中使用了应用程序主题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>

然而,它并不起作用。有什么想法吗?

9个回答

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

对我有用,点击这里查看详情。


4
谢谢。它对我有效。getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.material_blue_gray1))); - Ataru
根据我的经验,设置这个选项会有一个缺点,即操作栏菜单颜色不会改变。 - Elye
它运行得很完美,只需像@Ataru写的那样编辑注释即可。 - f.trajkovski

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

我已经用那个解决了。


4
我已经用这个解决了。 - zaptech

7
 ActionBar actionBar;

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

2
new ColorDrawable( 0x93E9FA ); - Ratata Tata

2

更新后的代码:

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

2

只需进入res/values/styles.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" />

希望这能对您有所帮助...


1
如果您使用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" />

你需要将以下代码放在你的Activity中

@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

点击按钮以获取并根据按钮的背景颜色更改操作栏和通知栏的颜色,希望这可以帮助到您。

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

colorlighter 用于将通知栏的颜色设置为比操作栏稍微浅一些。
color 是按钮 c2 的背景颜色,我们根据它来改变我们的颜色。


0
也许这个也能帮到你。它来自网站:

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

首先,您需要为您的应用程序(或活动,根据您的需求)声明一个自定义主题。类似于...
<!-- Somewhere in AndroidManifest.xml -->
<application ... android:theme="@style/ThemeSelector">

然后,为两种情况声明自定义主题,即具有和不具有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>

就是这样!

values-v14 文件夹怎么样? - Si8
你是什么意思,关于什么? - Bruno Bieri
对不起,我的意思是,我该如何使用Java而不是XML进行更改? - Si8
如果我理解正确的话,您需要在运行时更改应用程序主题,否则使用Java而不是XML定义就没有意义。为此,您可以在这里查看:https://dev59.com/k3E95IYBdhLWcg3wDpcG - Bruno Bieri

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接