简体   繁体   中英

Android Studio Material Design…styles.xml

I want to use the AppTheme android:Theme.Material.Light.DarkActionBar for my app. This is available from SDK 21...My minimum SDK is 16. I have the support library com.android.support:appcompat-v7:22.0.0 in my build gradle file. I want to create a second styles file under the the values folder. I want to have two styles files...one for Lollipop...greyed out version 22 next to it...and one for pre Lollipop. How to do this?

As said create a values folder for a specific Api Level

res/values-v21/styles.xml
res/values/styles.xml

In values/styles.xml create to styles:

<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/primaryColor</item>
    <item name="colorPrimaryDark">@color/primaryColorDark</item>
    <item name="colorAccent">@color/accentColor</item>
    <item name="android:windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
</style

<style name="AppTheme" parent="AppTheme.Base">
</style>

In values-v21/styles.xml create to styles:

<!-- Themes for Android API21 -->
<style name="AppTheme" parent="AppTheme.Base">
    <item name="android:colorPrimary">@color/primaryColor</item>
    <item name="android:colorPrimaryDark">@color/primaryColorDark</item>
    <item name="android:colorAccent">@color/accentColor</item>
</style>

Inside your AndroidManifest.xml you should use AppTheme as your global application theme

Further you should use the Toolbar from the SupportLibary instead of the default Actionbar. This is why the Base Theme sets the Actionbar to false

As suggested in the other answers you can use different styles.xml file in different folders.

I would like to add an option to other answers.

You can define a Base Style used by each versions, and customize singles attributes.

In res/values/styles.xml

<style name="AppTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/primaryColor</item>
    <item name="colorPrimaryDark">@color/primaryColorDark</item>
    <item name="colorAccent">@color/accentColor</item>
</style>

<style name="AppTheme" parent="AppTheme.Base">
   <!-- custom pre-Lollipop attributes -->
</style>

In res/values-v21/styles.xml

<style name="AppTheme" parent="AppTheme.Base">
   <!-- custom Lollipop attributes -->
</style>

Finally use the AppTheme in your Activity (you have to use a ` ActionBarActivity ).

Create two styles.xml files in different folders, eg:

res/values-v21/styles.xml
res/values/styles.xml

You should add another folder called "values-v21" in your "res" directory. Then create a "styles.xml" within that directory for your Lollipop styles.

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