Close your need to use a theme.appcompat Theme (or descendant) with this activity workaround

Source: Internet
Author: User

When my mainactivity inherits from Actionbaractivity or appcompatactivity in the V7 package, if you specify Style.xml in the Mainactivity file, the style used is as follows:

[Java]View Plain copy
  1. <style name="Apptheme" parent="Android:Theme.Material.NoActionBar" >
  2. <!-- 5.0, you can configure the app's style uniformly in the Style.xml file--
  3. <!--the color of the status bar--
  4. <item name="colorprimary" >@color/colorprimary</item>
  5. <!---level text color--
  6. <item name="Colorprimarydark" >@color/colorprimarydark</item>
  7. <!--the color of level two text--
  8. <item name="coloraccent" >@color/coloraccent</item>
  9. </style>

will report the following error:

Java.lang.IllegalStateException:You need to use a theme.appcompat Theme (or Descendatn) with this activity

So how do we solve this problem? Online a lot of life said will mainactivity to inherit from the activity can, but so the morning can not be compatible with the old version of the style, or can not be 5.0 before the version of the implementation of the Materialdesign effect, then how to correct the change it?

The steps to resolve are as follows:

1. Add a style style apptheme.base to the Res/styles.xml file, and then inherit the Apptheme from the Apptheme.base code as follows:

[HTML]View Plain copy
  1. <resources>
  2. <!--Base application theme.
  3. <style name= "apptheme" parent="Apptheme.base">
  4. <!--Customize your theme here--
  5. </style>
  6. <style name= "apptheme.base" parent="Theme.AppCompat.Light.NoActionBar">
  7. <item name="Colorprimary"> @color/colorprimary</Item>
  8. <item name="Colorprimarydark"> @color/colorprimarydark</Item>
  9. <item name="coloraccent"> @color/coloraccent</Item>
  10. <item name="Android:windowbackground"> @android: Color/white</Item>
  11. </style>
  12. </Resources>

2. Create the Values-v21 folder in the Res file, and then create the Styles.xml file under this folder, with the following code: [HTML]View Plain copy
  1. <? XML version= "1.0" encoding="Utf-8"?>
  2. <resources>
  3. <style name= "apptheme" parent="Apptheme.base">
  4. <item name="Android:colorprimary"> @color/colorprimary</Item>
  5. <item name="Android:colorprimarydark"> @color/colorprimarydark</Item >
  6. <item name="android:coloraccent"> @color/coloraccent</Item>
  7. </style>
  8. </Resources>
Description: The contents of the Values-v21 folder are specific to the configuration file used for versions above API21, that is, if the file before API21 is used res/ Styles.xml in values, otherwise use the Styles.xml under the Values-v21 folder


With these two steps, you can easily implement Mainactivity or inherit from Appcompatactivity, that is, you can run the material design effect on the API21 version of the phone, Also the style of API21 before and the API21 style can be decided by ourselves

Close your need to use a theme.appcompat Theme (or descendant) with this activity workaround

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.