5

我面臨一個問題,我嘗試了幾種方法來面對它,但仍不成功。上下文/覆蓋主題顏色

我的應用程序使用多個主題,如:萬聖節,聖誕節等,我使用TabLayout背景,文本顏色等小部件上的一些顏色屬性來上下文化應用程序。

問題是:如何根據主題上下文使用具有不同值的相同顏色屬性?

所以,基本上,這裏的正常方式申報的顏色:

<color name="mapMarkerSelectedTextColor">@android:color/white</color> 
<color name="mapLoadingIndicatorColor">@color/white</color> 

但是,主題和顏色不變所以我想,也許我可以覆蓋像每個主題內的那些顏色:

<item name="mapMarkerUnselectedTextColor">@color/christmas_red</item> 
    <item name="mapMarkerSelectedTextColor">@color/white</item> 

=>不成功

其他鉛,聲明這些顏色屬性:

<attr name="mapLoadingIndicatorColor" format="reference|color" /> 
<attr name="map_autocomplete_accent_color" format="reference|color" /> 

並在我的XML中使用主題像這樣:「?attr/mapLoadingIndicatorColor」。 但此功能只允許自棒棒堂版本並導致崩潰之前。

我已經閱讀了很多關於主題定製,顏色覆蓋,但從來沒有找到明確的解決方案關於這種情況。

無論如何。

回答

1

你提到:

而在我這樣的XML使用的主題: 「ATTR/mapLoadingIndicatorColor」。 但此功能只允許自棒棒糖版本並導致 之前崩潰。

我不知道是?attr/something不能使用前棒棒堂(棒棒堂有API等級21),因爲我用它在與仿真器API 16級設備,它工作正常。我用它像下面來改變按鈕的背景顏色,當選擇了不同的主題:

activity_main.xml中(在佈局文件夾):

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="A button" 
     style="?attr/myButton"/> 

</LinearLayout> 

在attrs.xml(在值文件夾):

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <attr name="myButton" format="reference"></attr> 
</resources> 

在styles.xml(中值文件夾):

<resources> 

<!-- default theme --> 
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 
    <item name="myButton">@style/defaultButtonStyle</item> 
</style> 

<style name="defaultButtonStyle" parent="android:Widget.Button"> 
    <item name="android:background">@color/green</item> 
</style> 

<!-- custom theme --> 
<style name="AppTheme.CustomTheme"> 
    <item name="myButton">@style/customButtonStyle</item> 
</style> 

<style name="customButtonStyle" parent="android:Widget.Button"> 
    <item name="android:background">@color/blue</item> 
</style> 

</resources> 

其實,我還是魁如果您可以指定從哪裏發現?attr/mapLoadingIndicatorColor將導致Lollipop崩潰的聲明,那麼這將非常棒! (我找不到任何地方,我只知道你不能使用提升屬性pre-Lollipop)