簡體   English   中英

在android.support.v7.app.ActionBar中更改背景顏色

[英]Change background color in android.support.v7.app.ActionBar

我對項目進行了一些更改,例如添加了android.support.v7.app.ActionBarDrawerToggle / ActionBar / ActionBarActivity / Toolbar 一切正常,我唯一的問題是我不知道如何更改ActionBar背景顏色,它是灰色,文本是黑色。 這是我嘗試的:

的themes.xml

<resources>
    <!-- the theme applied to the application or activity -->
    <style name="CustomActionBarTheme1" parent="@style/Theme.AppCompat.Light">
        <item name="windowActionBar">false</item>

        <item name="android:actionBarStyle">@style/MyActionBar</item>
        <item name="actionBarStyle">@style/MyActionBar</item>   
     </style>

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

</resources>

AndroidManifest.xml中

<application
        android:allowBackup="true"
        android:icon="@drawable/zenyt"
        android:label="@string/app_name"
        android:theme="@style/CustomActionBarTheme1">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >

並在strings.xml中添加了以下行:

<color name="blue">#FF2E4976</color>

我還嘗試以編程方式更改背景色:

ActionBar bar = getSupportActionBar(); 
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#FF2E4976")));

但是我得到這個錯誤:

 Caused by: java.lang.NullPointerException
            at com.zenyt.MainActivity.onCreate(MainActivity.java:67)

第67行是這樣的: bar.setBackgroundDrawable(newColorDrawable(Color.parseColor("#FF2E4976")));

另一種方式是這樣的:

<item name=”colorPrimary”>@color/my_awesome_red</item> 
<item name=”colorPrimaryDark”>@color/my_awesome_darker_red</item>

為了使用colorPrimary我需要將minSdk更改為21,但是我正在Galaxy S3(4.1.2)上測試該應用程序。 我當前的minSdk是16,我想保持這樣。 那么,有人可以幫我嗎?

編輯我決定也添加工具欄,也許我丟失了一些東西

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    app:theme="@style/ThemeOverlay.AppCompat.ActionBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary"/>

對不起我的英語不好。

在strings.xml中,使用color定義顏色

<color name="blue">#FF2E4976</color>

然后

 <style name="MyActionBar" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
    <item name="android:background">@color/blue</item>
    <item name="background">@color/blue</item>
</style>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM