Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/222.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 允许以样式为主题_Android - Fatal编程技术网

Android 允许以样式为主题

Android 允许以样式为主题,android,Android,我有一个简单的问题。我想为我的UI提供多个主题,用户可以在这些主题之间切换 问题是,我可以使用一个为基本UI类设置样式的主题,也可以直接在XML布局元素上使用一个样式,但是我不能定义一个根据我应用的主题而改变的样式。并不是我想要做的所有样式都是基于基元类型的 我想说的是,我想做的类似于使用CSS选择器来设置类或ID的样式,但是主题只允许您设置元素名称的样式 我现在能做的最好的事情就是拥有一个从我构建的实际样式中继承的基本样式,如下所示: My res/values/style.xml // A

我有一个简单的问题。我想为我的UI提供多个主题,用户可以在这些主题之间切换

问题是,我可以使用一个为基本UI类设置样式的主题,也可以直接在XML布局元素上使用一个样式,但是我不能定义一个根据我应用的主题而改变的样式。并不是我想要做的所有样式都是基于基元类型的

我想说的是,我想做的类似于使用CSS选择器来设置类或ID的样式,但是主题只允许您设置元素名称的样式


我现在能做的最好的事情就是拥有一个从我构建的实际样式中继承的基本样式,如下所示:

My 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>
//所有样式都必须在此处子类化才能成为通用样式,并且只有一种样式
//可以一次显示。基本上,我在做和设置相同的事情
//主题可以,但样式可以。如果我有50种风格,我必须有50种
//每个主题都有这些,一点也不干!
@安卓:颜色/背景颜色为深色
@可拉伸/割台纹理
my 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>

尝试以下方法:

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>

2) 将上述属性分配给您LinearLayout

<LinearLayout android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              style="?attr/myLinearLayoutStyle"
              android:gravity="center">
</LinearLayout>

3) 为此属性指定具体样式:

<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>

@样式/光背景
@风格/暗色背景
#D1CBF5
#351BE0

希望我正确理解了您的问题。

我还没有机会尝试这个方法,但这个解决方案看起来很有希望。谢谢,我已经用过你的密码了。但它有一个问题。必须从样式定义中删除“@attr/”。然后一切顺利。谢谢你。