繁体   English   中英

Android:ActionBar的自定义背景色

[英]Android: Custom Background Color for ActionBar

我已经阅读了许多有关此问题的文章,但没有任何帮助。 有人可以解释为什么我的项目不会更改操作栏的背景颜色吗? 文档说通过包括支持库兼容性

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

但是,我不知道如何只为可绘制对象指定颜色。 将#57d1a3放在此处会产生错误。 我什至不确定这能否解决我的问题。

主要活动:

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

表现:

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

主菜单:

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

最后,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>

尝试在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>

这似乎对我有用

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

您可以像这样制作一个简单的绘图

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

由于您使用AppCompat进行主题设置,因此答案非常简单。 不要使用操作栏,而是使用工具栏 下面是一个示例,说明如何使用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:

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

这将使工具栏的背景颜色为白色,标题为“您的标题”。

编辑:

这是我用于其中一个应用程序的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