簡體   English   中英

如何更改操作欄的背景顏色

[英]How to change the background colour of the action bar

我想更改操作欄的背景顏色,也可能更改文本顏色。 這個問題在這里已經問過,但我不理解。 我需要用這個做一個新的xml文件嗎?

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

    <style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
        <item name="android:background">ANY_HEX_COLOR_CODE</item>
    </style>
</resources>

那我應該在哪里保存呢? 什么文件夾? 以及如何將其設置為主題?

提前致謝。

您應該將此xml保存到values文件夾中,例如themes.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyTheme" parent="@android:style/Theme.Holo.Light">
    <item name="android:actionBarStyle">@style/MyActionBar</item>
</style>

<style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
    <item name="android:background">ANY_HEX_COLOR_CODE</item>
</style>
</resources>

您應該在AndroidManifest中選擇該主題,例如:

<application android:theme="@style/MyTheme">

嘗試這種方式...簡單地在drawable文件夾中創建一個新的顏色xml。

somecolor.xml

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

然后在onCreate中:

    getActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.somecolor));

如果您想更改文本顏色:

 int titleId = getResources().getIdentifier("action_bar_title", "id","android");
          TextView yourTextView = (TextView) findViewById(titleId);
       yourTextView.setTextColor(getResources().getColor(android.R.color.white));

這是更改操作欄顏色的方法(僅需幾個步驟):

  1. 使用Android操作欄樣式生成器為您的應用程序生成所有樣式xml。 這是一個網絡工具, http://android-ui-utils.googlecode.com/hg/asset-studio/dist/index.html 網絡允許您生成操作欄所需的樣式(顏色,背景等)。 這是Web工具的屏幕截圖。 在此處輸入圖片說明
  2. 該工具生成以下示例資源圖標,圖像,樣式xml。 將所有資源文件添加到應用程序資源可繪制區域,並更新AndroidManifest.xml application:theme以使用從工具生成的樣式xml。 以下是示例樣式,即使用該工具創建的圖像。 在此處輸入圖片說明
  3. 我將所有xml放入res / drawable文件夾中,除了操作欄樣式文件。 我將動作欄樣式文件放入res / values中。 樣式文件包含以下行: <style name="Theme.Example" parent="@android:style/Theme.Holo.Light.DarkActionBar">

  4. 更新您的AndroidManfest.xml,以在具有動作欄的主活動中使用動作欄樣式“ Theme.Example”。

     <activity android:theme="@style/Theme.Example" android:name="com.example.restaurant.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> 
  5. 以下是我為項目生成的樣式化的操作欄。 在此處輸入圖片說明

暫無
暫無

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

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