2011-06-08 19 views
14

我很難搞清楚如何在android中實現更復雜的主題/樣式情況。Android主題:在「baseTheme.xml」中定義顏色/漸變,在控件中使用,在「subThemeX.xml」中重寫

我研究了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> 

抽拉/ 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> 

抽拉/ 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> 

然後,我想如下定義樣式:

值/ MyBaseStyle.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <style name="MyBaseStyle" parent="@android:style/Theme.Light"> 
    </style>  
</resources> 

個值/ MySubStyle1.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <style name="MySubStyle1" parent="MyBaseStyle"> 
    </style> 
</resources> 

值/ MySubStyle2.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <style name="MySubStyle2" parent="MyBaseStyle"> 
    </style> 
</resources> 

最大的問題在這裏是:

1.我怎樣才能把一個漸變或顏色在MyBaseStyle.xml中,並在tab_bg_selected.xml和tab_bg_unselected.xml中使用它,而不是硬編碼的漸變/顏色?

2.如何覆蓋漸變/顏色我在MyBaseStyle.xml內分別MySubStyle1.xml和MySubStyle2.xml定義爲從,讓我的自定義tabwidget得到相應的風格?

備註:我真的希望能夠定義漸變/在分別MyBaseStyle.xml,MySubStyle1.xml和MySubStyle2.xml顏色(而不是多個不同XMLFILES內定義多個不同的顏色),以便能夠將「樣式」保存在一個文件中。這樣,我可以將我的應用程序的品牌外包。

有人可以幫我做到這一點嗎?

回答

1

在colors.xml在/ RES /值對於每個主題

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <color name="themeB_textColor">#e8e8e8</color> 
    <color name="themeA_textColor">#fff</color> 
</resources> 

爲視圖編程爲給定的主題然後設置顏色設置顏色?

//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);} 
} 
相關問題