You need to use a Theme. AppCompat theme (or descendant) with this activity solution,
When my MainActivity inherits the ActionBarActivity or AppCompatActivity from the v7 package, if you specify the style used by the MainActivity in the style. xml file as follows:
[Java] view plain copy
Print
@ Color/colorPrimary
@ Color/colorPrimaryDark
@ Color/col1_cent
The following error is reported:
Java. lang. IllegalStateException: You need to use a Theme. AppCompat theme (or descendatn) with this activity
So how can we solve this problem? On the Internet, many people say that you can replace MainActivity with the inherited Activity, but in this way, the style of the old version cannot be compatible in the morning, in other words, if MaterialDesign cannot be implemented in versions earlier than 5.0, how can we correct the modification?
The solution is as follows:
1. Add a new style AppTheme. Base in the res/styles. xml file, and inherit AppTheme from AppTheme. Base. The Code is as follows:
[Html] view plain copy
Print
@ Color/colorPrimary
@ Color/colorPrimaryDark
@ Color/col1_cent
@ Android: color/white
2. Create a values-v21 folder in the res file, and then create the styles. xml file under this folder, the Code is as follows:
[Html] view plain copy
Print
@ Color/colorPrimary
@ Color/colorPrimaryDark
@ Color/col1_cent
(Values-v21 folder content is specifically for API21 or later versions of the configuration files used, that is to say, if the API21 before the file is to use res/values styles. xml, otherwise use styles under the values-v21 folder. xml
Through the above two steps, you can easily implement MainActivity or inherit from AppCompatActivity. That is to say, you can run the Material Design effect on a mobile phone of a version earlier than API21, the style before API21 and after API21 can be determined by ourselves.