我有一个简单的问题.我想为我的UI提供多个主题,用户可以在这些主题之间切换
问题是我可以使用一个样式原始UI类的主题,或者我可以直接在XML布局元素上使用样式,但我无法定义一个样式,然后根据我应用的主题进行更改.并非我想要做的所有样式都基于原始类型.
我想说我想要做的就是使用CSS选择器来设置类或ID的样式,但主题只允许您设置元素名称的样式.
我现在能做的最好的是有一个基本风格,它继承了我构建的实际样式的EITHER,如下所示:
我的res/values/style.xml
// All styles have to be subclassed here to be made generic, and only one
// can be displayed at a time. Essentially, I'm doing the same thing as setting
// a theme does, but for styles. If I have 50 styles, I have to have 50 of
// these for each theme, and that's not DRY at all!
<style name="MyHeaderStyle" parent="MyHeaderStyle.Default"/>
<!--
<style name="MyHeaderStyle" parent="MyHeaderStyle.Textures"/>
-->
<style name="MyHeaderStyle.Default">
<item name="android:background">@android:color/background_dark</item>
</style>
<style name="MyHeaderStyle.Textures">
<item name="android:background">@drawable/header_texture</item>
</style>
Run Code Online (Sandbox Code Playgroud)
在layout.xml中的用法:
<!-- Note that I can't use a theme here, because I only want to style a
SPECIFIC element -->
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="@style/MyHeaderStyle"
android:gravity="center">
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
试试这个方法:
1)定义自己的res/values/attrs.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="AppTheme">
<attr name="myLinearLayoutStyle" format="reference" />
</declare-styleable>
</resources>
Run Code Online (Sandbox Code Playgroud)
2)将上面的attr分配给LinearLayout
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="?attr/myLinearLayoutStyle"
android:gravity="center">
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
3)为此attr分配具体样式:
<style name="AppLightTheme" parent="android:Theme.Holo.Light">
<item name="@attr/myLinearLayoutStyle">@style/lightBackground</item>
</style>
<style name="AppTheme" parent="android:Theme.Holo">
<item name="@attr/myLinearLayoutStyle">@style/darkBackground</item>
</style>
<style name="lightBackground">
<item name="android:background">#D1CBF5</item>
</style>
<style name="darkBackground">
<item name="android:background">#351BE0</item>
</style>
Run Code Online (Sandbox Code Playgroud)
希望我理解你的问题.
| 归档时间: |
|
| 查看次数: |
185 次 |
| 最近记录: |