Android:操作栏中的appcompat v7粗体标题

时间:2014-08-08 16:43:25

标签: android android-styles android-appcompat

我正在尝试使用ActionBar粗体制作标题,我想我已经拥有了所有必要的样式,但是它出现了这个错误消息:

  Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat th
eme (or descendant) with this activity.
    at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarAc
tivityDelegate.java:111)
    at android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBa
rActivityDelegateICS.java:58)
    at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.j
ava:98)
    at com.whatspoppin.android.whatspoppin.TestActivity.onCreate(TestActivit
y.java:58)

值 - > styles.xml:

 <resources xmlns:android="http://schemas.android.com/apk/res/android">

<!--
    Base application theme, dependent on API level. This theme is replaced
    by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
    <!--
        Theme customizations available in newer API levels can go in
        res/values-vXX/styles.xml, while customizations related to
        backward-compatibility can go here.
    -->
</style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    <item name="actionBarStyle">@style/MyTheme.MyActionBar</item>
</style>

  <style name="MyTheme.MyActionBar" parent="@style/Widget.AppCompat.Base.ActionBar">
    <item name="titleTextStyle">@style/MyTheme.MyActionBar.TitleTextStyle</item>
  </style>

  <style name="MyTheme.MyActionBar.TitleTextStyle" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
    <item name="android:textStyle">bold</item>
  </style>

在我的清单中:

  <activity 
         android:theme="@style/AppTheme"
         android:name=".TestActivity"
         android:screenOrientation="portrait"
         android:label="Example">
     </activity>

所以它告诉我我没有使用Theme.AppCompat主题或后代,但我的AppTheme的父级是AppBaseTheme,它的父级是Theme.AppCompat.Light.DarkActionBar。那么为什么抱怨这个呢?

完整错误:

  ng.RuntimeException: Unable to start activity ComponentInfo{com.whatspoppin.andr
 oid.whatspoppin/com.whatspoppin.android.whatspoppin.TestActivity}: java.lang.Ill
egalStateException: 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:2
230)
        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 th
eme (or descendant) with this activity.
    at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarAc
tivityDelegate.java:111)
    at android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBa
rActivityDelegateICS.java:58)
    at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.j
 ava:98)
    at com.whatspoppin.android.whatspoppin.TestActivity.onCreate(TestActivit
y.java:58)
    at android.app.Activity.performCreate(Activity.java:5104)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java
:1080)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:
2144)
    ... 11 more

再次编辑: 对不起,错误消失了。但是,我的头衔仍然不大胆。这就是我的全部:

值     

<!--
    Base application theme, dependent on API level. This theme is replaced
    by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
    <!--
        Theme customizations available in newer API levels can go in
        res/values-vXX/styles.xml, while customizations related to
        backward-compatibility can go here.
    -->
    <item name="actionBarStyle">@style/MyTheme.MyActionBar</item>
</style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">

</style>

  <style name="MyTheme.MyActionBar" parent="@style/Widget.AppCompat.Base.ActionBar">
    <item name="titleTextStyle">@style/MyTheme.MyActionBar.TitleTextStyle</item>
  </style>

  <style name="MyTheme.MyActionBar.TitleTextStyle" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
    <item name="android:textStyle">bold</item>
  </style>

值-V11:

 <resources>

<!--
    Base application theme for API 11+. This theme completely replaces
    AppBaseTheme from res/values/styles.xml on API 11+ devices.
-->
<style name="AppBaseTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
    <!-- API 11 theme customizations can go here. -->
    <item name="android:actionBarStyle">@style/MyTheme.MyActionBar</item>
</style>

  <style name="MyTheme.MyActionBar" parent="@style/Widget.AppCompat.Base.ActionBar">
    <item name="android:titleTextStyle">@style/MyTheme.MyActionBar.TitleTextStyle</item>
  </style>

  <style name="MyTheme.MyActionBar.TitleTextStyle" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
    <item name="android:textStyle">bold</item>
  </style>

值-V14:

  <resources>

<!--
    Base application theme for API 14+. This theme completely replaces
    AppBaseTheme from BOTH res/values/styles.xml and
    res/values-v11/styles.xml on API 14+ devices.
-->
<style name="AppBaseTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
    <!-- API 14 theme customizations can go here. -->
</style>

1 个答案:

答案 0 :(得分:1)

I'm testing in Api 17

<强>问题:

<item name="actionBarStyle">@style/MyTheme.MyActionBar</item>
<item name="titleTextStyle">@style/MyTheme.MyActionBar.TitleTextStyle</item>

actionBarStyletitleTextStyle仅用于较低级别的API,因为您使用更高级别的API来引用名称前的android:

<强>样品:

<item name="android:actionBarStyle">@style/MyTheme.MyActionBar</item>
<item name="android:titleTextStyle">@style/MyTheme.MyActionBar.TitleTextStyle</item>

编辑:

其中一个带有actionBarStyle值文件夹和其他主题android:actionBarStyle到v-11值文件夹