【问题标题】:Android action bar theme not applyingAndroid操作栏主题不适用
【发布时间】:2014-04-16 19:33:50
【问题描述】:

我最近开始了 android 开发,它通常没有问题。但是,尽管遵循信中的指南/说明(或者我可以说),但我似乎无法将主题应用于操作栏。

我使用 Android 操作栏样式生成器生成了我的主题,将 res 文件夹添加到我的项目中,并编辑了清单文件以使用新主题。我在 values 和 values-v14 中都有一个样式,后者使用“android:”。

预期结果 vs 当前结果

res/values/styles_amulettheme.xml:

<?xml version="1.0" encoding="utf-8"?>
    <!-- File created by the Android Action Bar Style Generator

         Copyright (C) 2011 The Android Open Source Project
         Copyright (C) 2012 readyState Software Ltd

         Licensed under the Apache License, Version 2.0 (the "License");
         you may not use this file except in compliance with the License.
         You may obtain a copy of the License at

              http://www.apache.org/licenses/LICENSE-2.0

         Unless required by applicable law or agreed to in writing, software
         distributed under the License is distributed on an "AS IS" BASIS,
         WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
         See the License for the specific language governing permissions and
         limitations under the License.
    -->

    <resources>

        <style name="Theme.Amulettheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
            <item name="actionBarItemBackground">@drawable/selectable_background_amulettheme</item>
            <item name="popupMenuStyle">@style/PopupMenu.Amulettheme</item>
            <item name="dropDownListViewStyle">@style/DropDownListView.Amulettheme</item>
            <item name="actionBarTabStyle">@style/ActionBarTabStyle.Amulettheme</item>
            <item name="actionDropDownStyle">@style/DropDownNav.Amulettheme</item>
            <item name="actionBarStyle">@style/ActionBar.Solid.Amulettheme</item>
            <item name="actionModeBackground">@drawable/cab_background_top_amulettheme</item>
            <item name="actionModeSplitBackground">@drawable/cab_background_bottom_amulettheme</item>
            <item name="actionModeCloseButtonStyle">@style/ActionButton.CloseMode.Amulettheme</item>

                    <!-- Light.DarkActionBar specific -->
            <item name="actionBarWidgetTheme">@style/Theme.Amulettheme.Widget</item>

        </style>

        <style name="ActionBar.Solid.Amulettheme" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
            <item name="background">@drawable/ab_solid_amulettheme</item>
            <item name="backgroundStacked">@drawable/ab_stacked_solid_amulettheme</item>
            <item name="backgroundSplit">@drawable/ab_bottom_solid_amulettheme</item>
            <item name="progressBarStyle">@style/ProgressBar.Amulettheme</item>
        </style>

        <style name="ActionBar.Transparent.Amulettheme" parent="@style/Widget.AppCompat.ActionBar">
            <item name="background">@drawable/ab_transparent_amulettheme</item>
            <item name="progressBarStyle">@style/ProgressBar.Amulettheme</item>
        </style>

        <style name="PopupMenu.Amulettheme" parent="@style/Widget.AppCompat.PopupMenu"> 
            <item name="android:popupBackground">@drawable/menu_dropdown_panel_amulettheme</item>   
        </style>

        <style name="DropDownListView.Amulettheme" parent="@style/Widget.AppCompat.ListView.DropDown">
            <item name="android:listSelector">@drawable/selectable_background_amulettheme</item>
        </style>

        <style name="ActionBarTabStyle.Amulettheme" parent="@style/Widget.AppCompat.ActionBar.TabView">
            <item name="android:background">@drawable/tab_indicator_ab_amulettheme</item>
        </style>

        <style name="DropDownNav.Amulettheme" parent="@style/Widget.AppCompat.Spinner.DropDown.ActionBar">
            <item name="android:background">@drawable/spinner_background_ab_amulettheme</item>
            <item name="android:popupBackground">@drawable/menu_dropdown_panel_amulettheme</item>
            <item name="android:dropDownSelector">@drawable/selectable_background_amulettheme</item>
        </style>

        <style name="ProgressBar.Amulettheme" parent="@style/Widget.AppCompat.ProgressBar.Horizontal">
            <item name="android:progressDrawable">@drawable/progress_horizontal_amulettheme</item>
        </style>

        <style name="ActionButton.CloseMode.Amulettheme" parent="@style/Widget.AppCompat.ActionButton.CloseMode">
            <item name="android:background">@drawable/btn_cab_done_amulettheme</item>
        </style>

        <!-- this style is only referenced in a Light.DarkActionBar based theme -->
        <style name="Theme.Amulettheme.Widget" parent="@style/Theme.AppCompat">
            <item name="popupMenuStyle">@style/PopupMenu.Amulettheme</item>
            <item name="dropDownListViewStyle">@style/DropDownListView.Amulettheme</item>
        </style>

    </resources>

AndroidManifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="quinn.anth.amulet"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.Amulettheme" >
        <activity
            android:name="quinn.anth.amulet.MenuActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

我们将不胜感激任何正确方向的帮助。

【问题讨论】:

  • 设法修复它感谢您的所有帮助。我有一些错误命名的图像,所以按下时使用了错误的图像。聚焦/点击的项目列在 selectable_background_amulettheme.xml 下。

标签: android android-layout


【解决方案1】:

对于最后一张图片,你需要在你的父样式中使用itemBackground属性如下:

<style name="Theme.Amulettheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
    <item name="android:itemBackground">@drawable/item_background</item>
</style>  

如果这能解决您的问题,请告诉我。

【讨论】:

  • 这已经解决了我的一个问题,但是我将如何更改单击/聚焦项目的颜色而不是更改所有背景?
  • 我不太清楚你的问题,你应该列出来。要更改单击/聚焦的颜色,我不知道这是否可以从样式中获得。但是,我想到了 onOptionsItemSelected 中的一种解决方法来更改带有 id 的图标。@AnthonyQuinn
【解决方案2】:

如果您使用 Android 支持库 .. 适用于 Android background,其他更高版本为android:background

像这样更改您的自定义主题

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:actionBarStyle">@style/MyActionBar</item>
</style>

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

【讨论】:

  • 正如我在帖子中提到的,我有两份关于样式的副本,一份使用 android: 用于 3+,另一份仅使用以前版本的变量名。我现在已经设法更改操作栏的颜色,但仍然找不到重音(单击和焦点)不起作用的原因。如果我从操作栏中选择一个项目,按钮将变为灰色而不是所需的橙色/金色。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-15
相关资源
最近更新 更多