Android 为各个样式标记指定颜色
是否可以为单个样式元素而不是在主题标记中指定colorPrimary和colorAccentAndroid 为各个样式标记指定颜色,android,android-layout,android-theme,android-styles,Android,Android Layout,Android Theme,Android Styles,是否可以为单个样式元素而不是在主题标记中指定colorPrimary和colorAccent <style name="MyCustomTabLayout" parent="android:Widget"> <item name="colorPrimaryDark">@color/gray900</item> <item name="colorAccent">@color/gray50</item>
<style name="MyCustomTabLayout" parent="android:Widget">
<item name="colorPrimaryDark">@color/gray900</item>
<item name="colorAccent">@color/gray50</item>
<item name="tabBackground">?attr/colorPrimaryDark</item>
<item name="tabSelectedTextColor">?android:colorAccent</item>
</style>
<style name="MyCustomTabLayout2" parent="android:Widget">
<item name="colorPrimaryDark">@color/gray50</item>
<item name="colorAccent">@color/gray900</item>
<item name="tabBackground">?attr/colorPrimaryDark</item>
<item name="tabSelectedTextColor">?android:colorAccent</item>
</style>
@颜色/灰色900
@颜色/灰色50
?属性/原色暗
?android:colorAccent
@颜色/灰色50
@颜色/灰色900
?属性/原色暗
?android:colorAccent
当我尝试这样做时,会应用默认主题。有没有更好的方法可以做到这一点?小部件在上下文主题中查找其重音颜色等。如果使用最新的appcompat支持库,您可以定义不同的主题来保存样式,并使用
android:theme
属性将它们应用于不同的布局。看
举一个例子:
res/values/themes.xml
<style name="RedThemeOverlay" parent="android:ThemeOverlay.Material">
<item name="android:colorEdgeEffect">#FF0000</item>
</style>
<ListView
...
android:theme="RedThemeOverlay" />
#FF0000
res/layout/fragment\u list.xml
<style name="RedThemeOverlay" parent="android:ThemeOverlay.Material">
<item name="android:colorEdgeEffect">#FF0000</item>
</style>
<ListView
...
android:theme="RedThemeOverlay" />