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
- <style name="Apptheme" parent="Android:Theme.Material.NoActionBar" >
- <!-- 5.0, you can configure the app's style uniformly in the Style.xml file--
- <!--the color of the status bar--
- <item name="colorprimary" >@color/colorprimary</item>
- <!---level text color--
- <item name="Colorprimarydark" >@color/colorprimarydark</item>
- <!--the color of level two text--
- <item name="coloraccent" >@color/coloraccent</item>
- </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
- <resources>
- <!--Base application theme.
- <style name= "apptheme" parent="Apptheme.base">
- <!--Customize your theme here--
- </style>
- <style name= "apptheme.base" parent="Theme.AppCompat.Light.NoActionBar">
- <item name="Colorprimary"> @color/colorprimary</Item>
- <item name="Colorprimarydark"> @color/colorprimarydark</Item>
- <item name="coloraccent"> @color/coloraccent</Item>
- <item name="Android:windowbackground"> @android: Color/white</Item>
- </style>
- </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
- <? XML version= "1.0" encoding="Utf-8"?>
- <resources>
- <style name= "apptheme" parent="Apptheme.base">
- <item name="Android:colorprimary"> @color/colorprimary</Item>
- <item name="Android:colorprimarydark"> @color/colorprimarydark</Item >
- <item name="android:coloraccent"> @color/coloraccent</Item>
- </style>
- </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