簡體   English   中英

樣式操作欄使用Theme.AppCompat

[英]styling action bar use a Theme.AppCompat

我正在關注Android培訓,在動作欄的樣式。

我因為android:theme=@style/MyActionBar面臨錯誤。

我想知道如何設置動作欄的樣式。

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

IllegalStateException:您需要將Theme.AppCompat主題與此活動一起使用

我認為錯誤來自:

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

我有以下錯誤:

錯誤:檢索項目的父項時出錯:找不到與給定名稱“@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>

編輯你的風格如下:

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

然后在你的清單中使用這個主題...在使用前不要忘記添加支持:如果你使用Android Studio,build.gradle中的appcompact依賴...感謝...

appcompat v7 libary有兩種文件。 一個是jar ,另一個是res ES樣string.xml styles.xml 您的問題暗示您沒有導入appcompat庫的res 因此,將.../Android-SDK/sdk/extras/android/support/v7/appcompat導入Eclipse,然后將其添加為主項目的libary項目。

我想你還沒有在你的項目中添加appcompat lib的引用。 將它的引用添加到您的項目並檢查

https://developer.android.com/tools/support-library/setup.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM