机器人主题:定义颜色/梯度"baseTheme.xml",控制中使用,在覆盖"subThemeX.xml"

Dir*_*irk 14 xml android themes branding custom-controls

我很难搞清楚如何在android中实现更复杂的主题/风格情况.

我已经研究过Android提供的不同的造型/主题教程,但是在我的情况下它们不符合要求.

该(蒸馏)情况如下:我创建了一个应用程序的定制tabwidget,我需要能够以品牌不同风格的应用程序(主题).

该XML的tabwidget(基于http://joshclemm.com/blog/?p=136):

布局/ 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="wrap_content"
            android:layout_height="wrap_content" 
            android:text="Hallowaaaaa"
            android:textSize="15dip"
            android:textColor="?android:textColorTertiary"/>
    </LinearLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

绘制/ 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>
Run Code Online (Sandbox Code Playgroud)

绘制/ 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>
Run Code Online (Sandbox Code Playgroud)

绘制/ 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>
Run Code Online (Sandbox Code Playgroud)

然后,我想定义样式如下:

值/ MyBaseStyle.xml

<?xml version="1.0" encoding="utf-8"?>
<resources> 
    <style name="MyBaseStyle" parent="@android:style/Theme.Light">
    </style>    
</resources>
Run Code Online (Sandbox Code Playgroud)

值/ MySubStyle1.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="MySubStyle1" parent="MyBaseStyle"> 
    </style>
</resources>
Run Code Online (Sandbox Code Playgroud)

值/ MySubStyle2.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="MySubStyle2" parent="MyBaseStyle"> 
    </style>
</resources>
Run Code Online (Sandbox Code Playgroud)

这里的重大问题是:

1.如何在MyBaseStyle.xml中放置渐变或颜色,并在tab_bg_selected.xml和tab_bg_unselected.xml中使用它而不是硬编码的渐变/颜色?

2.如何从MySubStyle1.xml和MySubStyle2.xml中分别覆盖我在MyBaseStyle.xml中定义的渐变/颜色,以便我的自定义tabwidget相应地设置样式?

备注:我真的希望能够分别在MyBaseStyle.xml,MySubStyle1.xml和MySubStyle2.xml中定义渐变/颜色(而不是在多个不同的XML文件中定义多种不同的颜色),以便能够保持"样式"在一个文件中.这样,我可以为我的应用程序外包品牌.

有人可以帮我完成这个吗?

Jac*_*ney 1

在/res/values的colors.xml中为每个主题设置颜色

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="themeB_textColor">#e8e8e8</color>
    <color name="themeA_textColor">#fff</color>
</resources>
Run Code Online (Sandbox Code Playgroud)

然后以编程方式为给定主题设置视图颜色?

//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);}
}
Run Code Online (Sandbox Code Playgroud)