How to add multiple theme attributes to the same a

2019-01-21 17:31发布

问题:

I have an android manifest with an activity that I want to apply to styles to:

 <activity android:label="@string/app_name" android:name="Language" android:theme="@android:style/Theme.NoTitleBar>

Is how it looks right now, but while keeping the NoTitleBar attribute, I would like to add this attribute as well:

android:style/Theme.Light"

But I'm just so new to Android that I can't figure it out.

Please help!

回答1:

You cannot have more than one theme applied at once in your manifest.

I believe there is a theme Theme.Light.NoTitleBar that will do what you want - but I will show you below how you can easily do this yourself and customize more.

What you need to do is create a theme which has either Theme.NoTitleBar or Theme.Light as it's parent and customizes the bits you want -- in this case the easiest way is to create a theme with Theme.Light as it's parent and just hide the title bar (rather than have the Theme.NoTitleBar as the parent and then have to make everything light which is much harder!).

You can do this with the following code in your themes.xml file in the values folder:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <!-- use the Android Light theme as the base for our own theme -->
    <style name="MySuperTheme" parent="@android:style/Theme.Light">

        <!-- hide the Window Title -->
        <item name="android:windowNoTitle">true</item>

        <!-- You could change the scrollbar, checkbox style, anything! -->
    </style>

</resources>

Then use android:theme="@style/MySuperTheme" for your activity (or you could even apply it to your whole application by placing it on the application element -- if you apply a style to an individual activity and have one set for the whole application as well then the style of the individual activity will be the one shown).

Take a look at the Android themes.xml for a list of all the things you can customize in your own theme.

You can also look at all of the Android styles to see how they are done.



回答2:

You'll need at least 2 styles, best inheriting from base styles, e.g. Theme.Material variants, or if you use appcompat then Theme.AppCompat variants. In each style override values such as colours, drawables etc with theme-specific values.

values/styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light">
    <!-- original theme attributes -->
    ...
    <item name="android:textColorPrimary">#FFFFFF</item>
</style>

<style name="AppTheme.Dark" parent="Theme.AppCompat">
    <!-- alternative theme attributes -->
    ...
    <item name="android:textColorPrimary">#000000</item>
</style>

This will be sufficient if you only use framework or appcompat attributes (e.g. colorAccent, android:textColorPrimary etc) in your layouts. But if you need your own attributes (e.g. a drawable with color that is different per theme), then you will need to define custom attributes.

values/attrs.xml

<attr name="themedMenuStoryDrawable" format="reference" />
<attr name="themedMenuCommentDrawable" format="reference" />
...

Specify theme-specific values for your custom attributes:

values/styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light">
    <!-- original theme attributes -->
    ...
    <item name="themedMenuStoryDrawable">@drawable/ic_subject_white_24dp</item>
    <item name="themedMenuCommentDrawable">@drawable/ic_mode_comment_white_24dp</item>
</style>

<style name="AppTheme.Dark" parent="Theme.AppCompat">
    <!-- alternative theme attributes -->
    ...
    <item name="themedMenuStoryDrawable">@drawable/ic_subject_black_24dp</item>
    <item name="themedMenuCommentDrawable">@drawable/ic_mode_comment_black_24dp</item>
</style>

Then refer to your custom attributes with ?attr/ prefix in layouts, menus etc:

menu/my_menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@id/menu_comment"
        android:icon="?attr/themedMenuCommentDrawable" />
    <item android:id="@id/menu_story"
        android:icon="?attr/themedMenuStoryDrawable" />
</menu>

Check out my blog post for the complete guide.