【问题标题】:how to set a theme to use ActionBar如何设置主题以使用 ActionBar
【发布时间】:2015-09-01 15:49:16
【问题描述】:

我正在尝试使用 ActionBar,我检查了一些帖子,并编写了以下代码,但在运行时我收到了以下 logCat 输出。我知道这是关于应用程序的主题,我尝试设置一个新主题,如下面的代码所示,但它也不起作用。

现在请告诉我如何解决它。

代码

private void initActionBar() {
    // TODO Auto-generated method stub
    //setTheme(android.R.style.Theme_Holo);
    getSupportActionBar().setTitle(R.string.action_discover_title);
}

style.xml

<resources>
<!--
    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="android:Theme.Light">
    <!--
        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. -->
</style>
</resources>

logcat

09-01 17:35:46.237: E/AndroidRuntime(19987): FATAL EXCEPTION: main
09-01 17:35:46.237: E/AndroidRuntime(19987): Process: com.example.ble_00, PID: 19987
09-01 17:35:46.237: E/AndroidRuntime(19987): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ble_00/com.example.ble_00.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
09-01 17:35:46.237: E/AndroidRuntime(19987):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2689)
09-01 17:35:46.237: E/AndroidRuntime(19987):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2754)
09-01 17:35:46.237: E/AndroidRuntime(19987):    at android.app.ActivityThread.access$900(ActivityThread.java:177)
09-01 17:35:46.237: E/AndroidRuntime(19987):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
09-01 17:35:46.237: E/AndroidRuntime(19987):    at android.os.Handler.dispatchMessage(Handler.java:102)

【问题讨论】:

    标签: android android-actionbar android-theme android-toolbar


    【解决方案1】:

    这个:

    <style name="AppBaseTheme" parent="android:Theme.Light">
        <!--
            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>
    

    应该是:

    <style name="AppBaseTheme" parent="Theme.AppCompat.Light">
        <!--
            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>
    

    【讨论】:

      【解决方案2】:

      因为您需要 Appcompat 主题才能使用 ActionBar。将 parent="android:Theme.Light" 更改为任何 Appcompat 主题作为您的外观和感觉偏好。 例如:

      <style name="AppBaseTheme" parent="Theme.AppCompat.Light">
          <!--
              can override the default look and feel of the parent theme
          -->
      </style>
      

      你可以在Styling ActionBar here看更多。

      并在您想要使用 Appcomapt 的 Activity 上使用该主题。您可以在 AndroidManifest.xml 文件中指定它

      <activity
              android:name="com.YourActivity"
              android:label="@string/label"
              android:theme="@style/AppBaseTheme"/>
      

      【讨论】:

        猜你喜欢
        • 2019-06-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多