【问题标题】:How to change the background colour of the action bar如何更改操作栏的背景颜色
【发布时间】:2014-03-26 15:21:56
【问题描述】:

我想更改操作栏的背景颜色,也可能更改文本颜色。这个问题已经问过here,但我不明白。 我需要用这个制作一个新的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>

我应该把它保存在哪里?什么文件夹? 以及如何将其设置为我的主题?

提前致谢。

【问题讨论】:

  • 不行,保存在res->value文件夹下的theme.xml文件中
  • 你可以输入各自的res/values/styles.xml

标签: java android xml android-activity android-actionbar


【解决方案1】:

您应该将此 xml 保存到 values 文件夹中,例如 theme.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">

【讨论】:

    【解决方案2】:

    试试这个方法...简单地在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));
    

    【讨论】:

      【解决方案3】:

      以下是更改操作栏颜色的方法(只需几个步骤):

      1. 使用 Android 操作栏样式生成器将所有样式 xml 用于 你的申请。这是一个网络工具, http://android-ui-utils.googlecode.com/hg/asset-studio/dist/index.html。 网络允许您生成样式(颜色、背景...等) 操作栏所必需的。这是网络的屏幕截图 工具。
      2. 工具生成样本 资源图标、图片、样式xml如下。全部添加 资源文件到应用程序资源可绘制区域,并更新 AndroidManifest.xml application:theme 使用 xml 生成的样式 从工具。以下是示例样式,创建的图像 从工具。
      3. 我把所有 xml 都放在 res/drawable 文件夹中,除了操作栏样式文件。我将操作栏样式文件放在 res/values 中。样式文件包含这一行: &lt;style name="Theme.Example" parent="@android:style/Theme.Holo.Light.DarkActionBar"&gt;

      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. 以下是我为我的项目生成的样式化操作栏。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-02-25
        • 2014-11-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多