我不能让Android ActionBar Custom主题工作(相信我,我已经尝试过)

时间:2015-02-05 23:34:42

标签: android android-actionbar styles themes android-appcompat

我确定我会因为缺乏特异性或提出被问到的问题而感到悲痛,但这确实是最后一种选择而且我真的尝试了太多解释所有的方法。这是我的问题:

我一直在阅读developer.android.com提供的标准教程,并且我已经完成了ActionBar的样式,几乎没有任何问题。那么现在我为那些缺乏问题买单并且我真的无法在行动栏上获得任何类型的自定义主题。

事实:

  • 我正在使用AppCompat-V7,因为我的应用程序非常简单,并且与尽可能多的版本完美兼容。
  • 我可以更改基本主题(Dark,Light和Light.DarkActionBar)
  • 我尝试过Android操作栏样式生成器,并在此处跟踪此句子(http://www.androiduipatterns.com/2012/09/creating-custom-android-styles-easy-way.html
  • 我已经观看了一些与我的问题/设置有些相关的视频,试图让它正确。
  • 我尝试了各种方法(都是错误的)让这个方法发挥作用,其中一些可能甚至没有意义。

那么......怎么样?

以下是我认为相关的文件,但如果有人想看到更多,请随时提问。考虑到我几乎没有通过Android教程,我没什么可看的,所以我认为这很好。

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="godwin.com.study" >


<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/CustomActionBarTheme" >

    <activity
        android:name=".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>

    <activity
        android:name=".DisplayMessageActivity"
        android:label="@string/title_activity_display_message"
        android:parentActivityName=".MainActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="godwin.com.study.MainActivity" />
    </activity>
</application>

</manifest>

styles.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
    </style>

</resources>

的themes.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <!-- the theme applied to the application or activity -->
    <style name="CustomActionBarTheme"
        parent="@style/Theme.AppCompat.Light.DarkActionBar">

        <!-- Support library compatibility -->
        <item name="actionBarStyle">@style/MyActionBar</item>
    </style>

    <!-- ActionBar styles -->
    <style name="MyActionBar"
        parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">

        <!-- Support library compatibility -->
        <item name="background">@drawable/actionbar_background</item>
    </style>



</resources>

的build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "godwin.com.study"
        minSdkVersion 8
        targetSdkVersion 18
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile "com.android.support:appcompat-v7:18.0+"
    compile fileTree(dir: 'libs', include: ['*.jar'])
}

随意提问,但要准备好&#34;我不知道&#34;。我很丢失。是的,我确实删除了很多尝试。它们相互重叠并互相干扰,所以我的文件中的内容确实有效,除了行:

<item name="background">@drawable/actionbar_background</item>
themes.xml 文件中的

。我把我的&#34; actionbar_background.png&#34;文件进入&#34; drawable&#34;文件夹,但它仍然只生成&#34; ... Light.DarkActionBar&#34;主题。

我真的希望它有点简单,但通过我所有的搜索,我还没能找到答案。

单面注意:这是我第一次开发Android应用程序,或者至少尝试过,我真的不认为Android在他们的教程中做得很好。我的意思是,我在编程上很体面。一直在使用C ++,还有一些Java,Javascript和网页设计语言,我大部分时间都在使用它们。只有我吗?或进入应用程序开发的世界真的很困难?

编辑:为了试着简单地说,我尝试了很多方法来改变动作栏的外观并使用自定义主题(无论是使用颜色代码还是图像/ .pngs),但是只有通过使用预设主题(Light,Dark和Light.DarkActionBar)才能让它改变。

1 个答案:

答案 0 :(得分:2)

  

我希望能够使用我提到的Android Action Bar Style Generator,

如网站上所述,该生成器已弃用,因为您不需要使用AppCompat操作栏。如果您的目标是为操作栏提供自定义背景颜色,并且您使用当前版本的AppCompat(appcompat-v7,版本21或更高版本),则可以通过colorPrimary执行此操作。

例如,this sample app在操作栏上设置自定义颜色。它有res/values/colors.xml定义了一些颜色:

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <color name="primary">#3f51b5</color>
  <color name="primary_dark">#1a237e</color>
  <color name="accent">#ffee58</color>
</resources>

它有一个res/values/styles.xml,将primary颜色应用于colorPrimary主题属性:

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <style name="Theme.Apptheme" parent="Theme.AppCompat">
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primary_dark</item>
    <item name="colorAccent">@color/accent</item>
  </style>
</resources>

它在清单中应用自定义主题:

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

    <supports-screens
        android:anyDensity="true"
        android:largeScreens="true"
        android:normalScreens="true"
        android:smallScreens="true"/>

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="19"/>

    <application
        android:allowBackup="false"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.Apptheme">
        <activity
            android:name="ActionBarDemoActivity"
            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>

你得到:

AppCompat Action Bar, with Custom Theme, on Android 4.3