如何在Android Studio中为Android项目设置自定义主题?

时间:2014-12-18 11:49:08

标签: android android-studio themes

我为我的android项目制作了一个自定义主题:Android Action Bar Style Generator

但是,使用Android Studio时程序会发出错误。 我的项目图片:see image

LogCat错误:

12-18 11:21:59.340    4466-4466/com.example.mohammad.myapplication2 E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mohammad.myapplication2/com.example.mohammad.myapplication2.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
        at android.app.ActivityThread.access$600(ActivityThread.java:141)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5041)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
        at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:151)
        at android.support.v7.app.ActionBarActivityDelegateBase.onCreate(ActionBarActivityDelegateBase.java:138)
        at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:123)
        at com.example.mohammad.myapplication2.MainActivity.onCreate(MainActivity.java:15)
        at android.app.Activity.performCreate(Activity.java:5104)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)

3 个答案:

答案 0 :(得分:0)

从Android操作栏样式生成器生成主题时,选择样式兼容性为AppCompat,然后重新生成。我猜你必须把它留给默认的Holo,这是创造这个错误

使用AppCOmpat主题重新生成可以解决您的问题

答案 1 :(得分:0)

我的清单代码:

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/Theme.Tellfaactionbarstyle2">
    <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=".MainActivity2"
        android:label="@string/title_activity_main_activity2">
    </activity>
    <activity
        android:name=".cal_activity"
        android:label="@string/title_activity_cal_activity">
    </activity>
</application>

和我的样式代码:

<resources>

<style name="Theme.Tellfaactionbarstyle2" parent="@style/Theme.AppCompat.Light">
    <item name="actionBarItemBackground">@drawable/selectable_background_tellfaactionbarstyle2</item>
    <item name="popupMenuStyle">@style/PopupMenu.Tellfaactionbarstyle2</item>
    <item name="dropDownListViewStyle">@style/DropDownListView.Tellfaactionbarstyle2</item>
    <item name="actionBarTabStyle">@style/ActionBarTabStyle.Tellfaactionbarstyle2</item>
    <item name="actionDropDownStyle">@style/DropDownNav.Tellfaactionbarstyle2</item>
    <item name="actionBarStyle">@style/ActionBar.Solid.Tellfaactionbarstyle2</item>
    <item name="actionModeBackground">@drawable/cab_background_top_tellfaactionbarstyle2</item>
    <item name="actionModeSplitBackground">@drawable/cab_background_bottom_tellfaactionbarstyle2</item>
    <item name="actionModeCloseButtonStyle">@style/ActionButton.CloseMode.Tellfaactionbarstyle2</item>


</style>

<style name="ActionBar.Solid.Tellfaactionbarstyle2" parent="@style/Widget.AppCompat.Light.ActionBar.Solid">
    <item name="background">@drawable/ab_solid_tellfaactionbarstyle2</item>
    <item name="backgroundStacked">@drawable/ab_stacked_solid_tellfaactionbarstyle2</item>
    <item name="backgroundSplit">@drawable/ab_bottom_solid_tellfaactionbarstyle2</item>
    <item name="progressBarStyle">@style/ProgressBar.Tellfaactionbarstyle2</item>
</style>

<style name="ActionBar.Transparent.Tellfaactionbarstyle2" parent="@style/Widget.AppCompat.Light.ActionBar">
    <item name="background">@drawable/ab_transparent_tellfaactionbarstyle2</item>
    <item name="progressBarStyle">@style/ProgressBar.Tellfaactionbarstyle2</item>
</style>

<style name="PopupMenu.Tellfaactionbarstyle2" parent="@style/Widget.AppCompat.Light.PopupMenu"> 
    <item name="android:popupBackground">@drawable/menu_dropdown_panel_tellfaactionbarstyle2</item> 
</style>

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

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

<style name="DropDownNav.Tellfaactionbarstyle2" parent="@style/Widget.AppCompat.Light.Spinner.DropDown.ActionBar">
    <item name="android:background">@drawable/spinner_background_ab_tellfaactionbarstyle2</item>
    <item name="android:popupBackground">@drawable/menu_dropdown_panel_tellfaactionbarstyle2</item>
    <item name="android:dropDownSelector">@drawable/selectable_background_tellfaactionbarstyle2</item>
</style>

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

<style name="ActionButton.CloseMode.Tellfaactionbarstyle2" parent="@style/Widget.AppCompat.Light.ActionButton.CloseMode">
    <item name="android:background">@drawable/btn_cab_done_tellfaactionbarstyle2</item>
</style>

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

答案 2 :(得分:0)

进入Gradle,在Block Dependencies:

中插入此行

编译'com.android.support:appcompat-v7:18.0。+'

现在,您的项目运行成功,

祝你好运。