I'm trying to work out how to organize an android application that will have multiple themes. here's a simplified example of my problem,
2 themes, "light" and "dark",
within each theme, two text colors: "enabled" and "disabled"now the problem is that when i define my TextView, i don't want to call out "light" or "dark" there, i want to just specify the theme at the application level and have it applied. basically, i want CSS selectors. i want to be able to define my theme like,
<style name="Light.enabled" .../>
<style name="Light.disabled" .../>
and my text view like,
<TextView style="@style/.enabled" .../>
<TextView style="@style/.disabled" .../>
and have it apply "enabled" or "disabled" based on whether i've called out "light" or "dark" at the application level.
this page, http://www.androidengineer.com/2010/06/using-themes-in-android-applications.html
shows an app that
defines a style, say "light.enabled"
#000000
defines an attribute reference, say "enabled"
defines a style (theme) item like,
@style/light.enabled
uses the attr to define the style in the view,
this is what i want, but it doesn't work for me. the only diff is that i'm using an appwidget. the author sets the theme on the activity. the only place i can set it is Context.setTheme(), and at the "application" tag in the manifest. neither of which se开发者_Python百科ems to make a difference.
You can't apply themes to app widgets. You'll just need to have different XML that uses different styles.
Also, it is confusing when you talk about light vs. dark and enabled vs. disabled as similar things. They are very different in the platform.
Light and dark are actual "themes" as the platform defines it, which is a set of default values for resource attributes, rooted off of android:style/Theme. These are changed with android:theme in the manifest or setTheme() in the API.
enabled and disabled are states. They are used with StateListDrawable (via tag in drawable/) or ColorStateList (via tag in color/) to pick a drawable/color based on the enabled state.
For example here is a color that changes based on state:
https://android.googlesource.com/platform/frameworks/base/+/master/core/res/res/color/primary_text_dark.xml
And here is a drawable that changes based on state:
https://android.googlesource.com/platform/frameworks/base/+/master/core/res/res/drawable/btn_default.xml
If you are trying support multiple themes in application.
Define theme in values/styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light">
@drawable/ic_mode_comment_white_24dp
... @drawable/ic_subject_black_24dp
define colors in colors.xml
<resources>
<color name="colorPrimary">#FF9800</color>
</resources>
switch themes in activity runtime
public abstract class BaseActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { if (PreferenceManager.getDefaultSharedPreferences(this) .getBoolean("pref_dark_theme"), false)) { setTheme(R.style.AppTheme_Dark); } super.onCreate(savedInstanceState); } }
follow this articles part1 and part2
I add many more themes to the android app. But work only if-else statements.
My code:
if (sharedPreferences.getString("color_option", "Blue").equals("Blue")) {
setTheme(R.style.RedTheme);
} else {
setTheme(R.style.BlueTheme);
}
Next Step
if (sharedPreferences.getString("color_option", "Blue").equals("Blue")) {
setTheme(R.style.RedTheme);
} else if(sharedPreferences.getString("color_option", "Blue").equals("Blue")) {
setTheme(R.style.BlueTheme);
} else {
setTheme(R.style.HTheme);
}
Both Code does not work only Selected two themes not selected the third theme (Not Work). I add multiple or more themes.
精彩评论