[英]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));
這是更改操作欄顏色的方法(僅需幾個步驟):
我將所有xml放入res / drawable文件夾中,除了操作欄樣式文件。 我將動作欄樣式文件放入res / values中。 樣式文件包含以下行: <style name="Theme.Example" parent="@android:style/Theme.Holo.Light.DarkActionBar">
更新您的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>
以下是我為項目生成的樣式化的操作欄。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.