2015-04-03 46 views
3

我正在關注Android培訓,在造型操作欄中。造型操作欄使用Theme.AppCompat

我面臨錯誤,因爲android:[email protected]/MyActionBar

我想知道如何風格的行動欄。

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/MyActionBar" > 

IllegalStateException: You need to use a Theme.AppCompat theme with this activity

我認爲該錯誤:

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

,所以我將它修改爲

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

我有以下錯誤:

error: Error retrieving parent for item: No resource found that matches the given name '@android:style/Theme.AppCompat.Light '.

我創建了一個新的文件作爲培訓解釋

RES /價值/的themes.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <!-- the theme applied to the application or activity --> 
    <style name="CustomActionBarTheme" 
      parent="@android:style/Theme.Holo.Light.DarkActionBar"> 
     <item name="android:actionBarStyle">@style/MyActionBar</item> 
    </style> 

    <!-- ActionBar styles --> 

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

這裏是清單

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

    <uses-sdk 
     android:minSdkVersion="16" 
     android:targetSdkVersion="21" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/MyActionBar" > 
     <activity 
      android:name="com.example.helloworld.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="com.example.helloworld.DisplayMessageActivity" 
     android:label="@string/title_activity_display_message" 
     android:parentActivityName="com.example.Helloworld.MainActivity" > 
     <meta-data 
      android:name="android.support.PARENT_ACTIVITY" 
      android:value="com.example.Helloworld.MyActivity" /> 
    </activity> 

    </application> 

</manifest> 
+0

您是否已將庫添加到項目屬性中? – Harry 2015-04-03 11:27:47

回答

2

appcompat v7 libary有兩種類型的文件。一個是jar,另一個是res es像string.xmlstyles.xml。你的問題意味着你沒有導入appcompat庫的res es。因此,將.../Android-SDK/sdk/extras/android/support/v7/appcompat的項目導入到Eclipse中,然後將其添加爲主項目的庫文件項目。

+0

如何導入庫?我在Android網站閱讀'suppoert library Features',但是我不知道如何導入它 – Baalback 2015-04-03 11:22:34

+0

在你的''Eclipse','File' - >'Import' - >'Android' - >'現有的Android代碼到Workspace中' - >'Next' - >'Browse' - >然後選擇'.../Android-SDK/sdk/extras/android/support/v7/appcompat' - >'OK' - >'Finish' 。當我到達現有的Android代碼到工作區時 – SilentKnight 2015-04-03 11:26:36

+0

..它是空的,我搜索了我的項目,我添加它,我仍然無法點擊插入,順便說一句我發現在我的項目上面的eclipse有appcompat_v7 @silentKnight – Baalback 2015-04-03 11:40:24

3

編輯自己的風格如下:

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

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

<!-- ActionBar styles --> 
<style name="MyActionBar" 
    parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse"> 
    <item name="android:background">@drawable/actionbar_background</item> 

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

支持庫,你必須使用的

name="background">@drawable/actionbar_background</item> 

代替

name="android:background">@drawable/actionbar_background</item> 

,然後在您的清單中使用這個主題..使用前請不要忘記添加支持:build.gradle中的appcompact依賴關係您正在使用Android Studio ... 謝謝...