简体   繁体   中英

How to apply Material theme in Android?

So i'm trying to apply material theme to a newly created app. As per android documentation:

"To use material design in your Android apps, follow the guidelines described in the material design specification and use the new components and functionality available in Android 5.0 (API level 21)."

What i did was create a new project with:

minSdkVersion 23 targetSdkVersion 25

Then i went into the styles.xml and changed the style to:

<style name="AppTheme" parent="@android:style/Theme.Material">

Then i try to build on a emulated device with the following configuration:

nexus 5/api 25.

The app crashes with: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

I don't understand why? Haven't i met the conditions required? My minimum target sdk is right as per documentation, and the device i am building has the right sdk as well.

What am i missing?

You are using an AppCompatActivity not a framework Activity

As such, the theme you need to use should have an AppCompat theme as its parent such as

<style name="AppTheme" parent="@style/Theme.AppCompat.Light">
</style>

For using material theme make your MainActivity (or the activity for which you are applying the theme) a subclass of Activity , not AppCompatActivity or ActionBarActivity . You should also check this .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM