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