I'm having a hard time figuring out how to implement a more complex Theme/Style situation in android.
I've studied the different Styling/Theming tutorials provided by Android, but they don't fit the bill in my case.
The (distilled) situation is the following: I'm creating an application with a custom tabwidget, and I need to be able to brand the application with different styles (themes).
The XML for the tabwidget (Based on http://joshclemm.com/blog/?p=136):
layout/tabs_bg.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabsLayout" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="@drawable/tab_bg_selector"
android:padding="10dip" android:gravity="center" android:orientation="vertical">
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content" android:orientation="horizontal"
android:gravity="center">
<ImageView
android:src="@drawable/star_fav_empty"
android:layout_height="24px"
android:layout_width="24px"
android:id="@+id/tabsImage"
android:paddingRight="5dip"></ImageView>
<TextView
android:id="@+id/tabsText"
android:layout_width开发者_Go百科="wrap_content"
android:layout_height="wrap_content"
android:text="Hallowaaaaa"
android:textSize="15dip"
android:textColor="?android:textColorTertiary"/>
</LinearLayout>
</LinearLayout>
drawable/tab_bg_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Active tab -->
<item android:state_selected="true" android:state_focused="false"
android:state_pressed="false" android:drawable="@drawable/tab_bg_selected" />
<!-- Inactive tab -->
<item android:state_selected="false" android:state_focused="false"
android:state_pressed="false" android:drawable="@drawable/tab_bg_unselected" />
<!-- Pressed tab -->
<item android:state_pressed="true" android:state_enabled="false" android:drawable="@android:color/transparent" />
<!-- Selected tab (using d-pad) -->
<item android:state_focused="true" android:state_selected="true"
android:state_pressed="false" android:drawable="@android:color/transparent" />
</selector>
drawable/tab_bg_selected.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:startColor="#A8A8A8" android:centerColor="#7F7F7F"
android:endColor="#696969" android:angle="-90" />
</shape>
drawable/tab_bg_unselected.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:startColor="#5C5C5C" android:centerColor="#424242"
android:endColor="#222222" android:angle="-90" />
</shape>
Then, I would like to define styles as follows:
values/MyBaseStyle.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyBaseStyle" parent="@android:style/Theme.Light">
</style>
</resources>
values/MySubStyle1.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MySubStyle1" parent="MyBaseStyle">
</style>
</resources>
values/MySubStyle2.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MySubStyle2" parent="MyBaseStyle">
</style>
</resources>
The big questions here are:
1. How can I put a gradient or a color in MyBaseStyle.xml, and use it within tab_bg_selected.xml and tab_bg_unselected.xml instead of the hardcoded gradient/color?
2. How can I override the gradient/color I defined in MyBaseStyle.xml from within MySubStyle1.xml and MySubStyle2.xml respectively, so that my custom tabwidget gets styled accordingly?
Remark: I would really like to be able to define the gradient/colors in respectively MyBaseStyle.xml, MySubStyle1.xml and MySubStyle2.xml (as opposed to defining multiple different colors within multiple different XMLfiles) to be able to keep the "styling" within one file. That way, I can outsource the branding for my application.
Can someone please help my accomplish this?
In the colors.xml in /res/values set colors for each theme
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="themeB_textColor">#e8e8e8</color>
<color name="themeA_textColor">#fff</color>
</resources>
Then set color for views programmatically for the given theme?
//at on create grab the selected theme however it has been set - ie: through preferences
if(theme=='themeA')
{
super.setTheme(R.style.ThemeA);
Color textColor = getResources().getColor(R.color.themeA_textColor);
}
//later
applyTextColors(textView1,textView2...)
//make a function for applying colors
public void applyTextColors(TextView... tvs)
{
for(TextView tv : tvs){tv.setTextColor(textColor);}
}
精彩评论