You need to use a Theme. AppCompat theme (or descendant) with this activity solution,

Source: Internet
Author: User

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.

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.