简体   繁体   English

Android:ActionBar的自定义背景色

[英]Android: Custom Background Color for ActionBar

I've read many posts on this problem, but none have helped. 我已经阅读了许多有关此问题的文章,但没有任何帮助。 Can someone explain why my project will not change the background color of my Action Bar? 有人可以解释为什么我的项目不会更改操作栏的背景颜色吗? The docs say to include support library compatibility, via 文档说通过包括支持库兼容性

<item name="background">@drawable/actionbar_background</item>, 

however, I don't know how to specify just a color for the drawable. 但是,我不知道如何只为可绘制对象指定颜色。 Putting #57d1a3 here gives an error. 将#57d1a3放在此处会产生错误。 I'm not even sure this would solve my problem. 我什至不确定这能否解决我的问题。

MainActivity: 主要活动:

public class MainActivity extends ActionBarActivity {

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


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

Manifest: 表现:

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

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/CustomActionBarTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        android:theme="@style/CustomActionBarTheme" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

Main Menu: 主菜单:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
    <item android:id="@+id/action_settings" android:title="@string/action_settings"
    android:orderInCategory="100" app:showAsAction="never" />
</menu>

And finally, styles.xml: 最后,styles.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">
    <item name="android:actionBarStyle">@style/MyActionBar</item>
</style>

<!-- ActionBar styles -->
<style name="MyActionBar"
    parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
    <item name="android:background">#57d1a3</item>
</style>
</resources>

Try changing your code in styles.xml: 尝试在styles.xml中更改代码:

<resources xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android">
<!-- the theme applied to the application or activity -->
  <style name="CustomActionBarTheme"
      parent="@style/Theme.AppCompat.Light">
      <item name="android:actionBarStyle" tools:ignore="NewApi">@style/MyActionBar</item>
  </style>

<!-- ActionBar styles -->
  <style name="MyActionBar"
      parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
      <item name="android:background" tools:ignore="NewApi">#57d1a3</item>
       <item name="background" tools:ignore="NewApi">@color/myColor</item>
  </style>
</resources>

This seems to work for me 这似乎对我有用

<resources>
    <style name="MyTheme" parent="@android:style/Theme.AppCompat.Light">
        <item name="android:actionBarStyle">@style/MyActionBar</item>
    </style>

    <style name="MyActionBar" parent="@android:style/Widget.AppCompat.Light.ActionBar.Solid">
        <item name="android:background">#FF0000</item>
    </style>
</resources>

You can make a simple drawable like this 您可以像这样制作一个简单的绘图

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle">
            <solid android:color="#YOUR_OWN_COLOR"/> 
</shape>

Since you're using AppCompat for theming, the answer is really simple. 由于您使用AppCompat进行主题设置,因此答案非常简单。 Don't use an Action Bar but instead, a ToolBar . 不要使用操作栏,而是使用工具栏 Below is an example of how to define a Toolbar in XML, and set it up in your activity with color. 下面是一个示例,说明如何使用XML定义工具栏,并在您的活动中使用颜色对其进行设置。

toolbar.xml: 工具栏.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fb_toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize" />

YourActivity.java: YourActivity.java:

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

    Toolbar tb = (Toolbar) findViewById(R.id.fb_toolbar);
    setSupportActionBar(tb);

    //Parse a color with any hex code
    tb.setBackgroundColor(Color.parseColor("#FFFFFF"));
    //Set title
    tb.setTitle("Your title");
}

This should make a Toolbar with a background color of white and a title of "Your Title". 这将使工具栏的背景颜色为白色,标题为“您的标题”。

Edit: 编辑:

Here's a snippet of styles.xml which I use for one of my apps. 这是我用于其中一个应用程序的styles.xml的片段。 It uses a toolbar and navigation drawer so some of it can be ignored. 它使用工具栏和导航抽屉,因此其中一些可以忽略。

styles.xml: styles.xml:

<style name="AppThemeNavDrawer" parent="Theme.AppCompat.NoActionBar">
    <item name="colorAccent">#F8F8F8</item>
    <item name="windowActionModeOverlay">true</item>
</style>

<style name="ToolbarCustomIconColor" parent="AppThemeNavDrawer">
    <item name="android:textColorSecondary">#FFFFFF</item>
    <item name="android:activatedBackgroundIndicator">@drawable/drawer_list_selector</item>
</style>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM