Android - 如何以编程方式设置主题值(从数据库值)

时间:2018-02-01 10:12:16

标签: android xamarin

如果我的主题值可以从服务器下载,那么我的移动主题会根据我指定的内容进行更改吗?

示例如果我有来自服务器的列[colorPrimary]和[colorAccent],那么在下载值之后,我的应用主题颜色会相应地改变。

这是我目前的主题。

<style name="Base.Theme.Design" parent="Theme.AppCompat.Light.NoActionBar">
  <item name="colorPrimary">#013034</item>
  <item name="colorPrimaryDark">#013034</item>
  <item name="colorAccent">#1490a0</item>
  <item name="android:textColorHint">#9e9e9e</item>
</style>

注意:对于所有downvotes,请留下评论以改进此帖子。感谢。

2 个答案:

答案 0 :(得分:0)

您可以使用Firebase远程配置来应用它,例如,如果您的应用有多个主题选项,例如&#34; Base.Theme.Design_A&#34;和&#34; Base.Theme.Design_B&#34;这些已经内置在你的应用程序中。您可以通过检查Firebase远程配置中的远程属性来切换和应用其中一个主题。您还可以更改旧值并获取远程值并激活它们(不使用样式文件)

<soap:Envelope
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
    <ns2:getCardListResponse
        xmlns:ns2="http://soap.services.cmdbuild.org">
        <ns2:return>
            <ns2:cards>
                <ns2:attributeList>
                    <ns2:name>instance_id</ns2:name>
                    <ns2:value>5919</ns2:value>
                </ns2:attributeList>
                <ns2:attributeList>
                    <ns2:name>Description</ns2:name>
                    <ns2:value>CentOS-7-x86-64-1708</ns2:value>
                </ns2:attributeList>
                <ns2:attributeList>
                    <ns2:name>instance_name</ns2:name>
                    <ns2:value>i-2-5919-VM</ns2:value>
                </ns2:attributeList>
                <ns2:attributeList>
                    <ns2:name>hypervisor_hostname</ns2:name>
                    <ns2:value>ct-hn-v-4754875487</ns2:value>
                </ns2:attributeList>
                <ns2:attributeList>
                    <ns2:name>private_mac_address</ns2:name>
                    <ns2:value>1e:00:0f:00:04:97</ns2:value>
                </ns2:attributeList>
                <ns2:attributeList>
                    <ns2:name>base_hostname</ns2:name>
                    <ns2:value></ns2:value>
                </ns2:attributeList>
                <ns2:attributeList>
                    <ns2:name>cloudstack_name</ns2:name>
                    <ns2:value></ns2:value>
                </ns2:attributeList>
                <ns2:attributeList>
                    <ns2:name>private_ip_address</ns2:name>
                    <ns2:value>10.0.215.48</ns2:value>
                </ns2:attributeList>
                <ns2:attributeList>
                    <ns2:name>Code</ns2:name>
                    <ns2:value></ns2:value>
                </ns2:attributeList>
                <ns2:attributeList>
                    <ns2:name>uuid</ns2:name>
                    <ns2:value>d54d08d0-83ef-47bb-b4aa-f0b2c8105639</ns2:value>
                </ns2:attributeList>
                <ns2:attributeList>
                    <ns2:name>Notes</ns2:name>
                    <ns2:value></ns2:value>
                </ns2:attributeList>
                <ns2:beginDate>2018-01-24T12:00:00.031+02:00</ns2:beginDate>
                <ns2:className>cloudstack</ns2:className>
                <ns2:id>1540303</ns2:id>
            </ns2:cards>
            <ns2:cards>
                <ns2:attributeList>
                    <ns2:name>instance_id</ns2:name>
                    <ns2:value>7259</ns2:value>
                </ns2:attributeList>
                <ns2:attributeList>
                    <ns2:name>Description</ns2:name>
                    <ns2:value>CSCM-WLP-INTL-20GB</ns2:value>
                </ns2:attributeList>
                <ns2:attributeList>
                    <ns2:name>instance_name</ns2:name>
                    <ns2:value>i-92-7259-VM</ns2:value>
                </ns2:attributeList>
                <ns2:attributeList>
                    <ns2:name>hypervisor_hostname</ns2:name>
                    <ns2:value>ct-hn-v-85847487584</ns2:value>
                </ns2:attributeList>
                <ns2:attributeList>
                    <ns2:name>private_mac_address</ns2:name>
                    <ns2:value>1e:00:1e:00:04:7a</ns2:value>
                </ns2:attributeList>
                <ns2:attributeList>
                    <ns2:name>base_hostname</ns2:name>
                    <ns2:value></ns2:value>
                </ns2:attributeList>
                <ns2:attributeList>
                    <ns2:name>cloudstack_name</ns2:name>
                    <ns2:value></ns2:value>
                </ns2:attributeList>
                <ns2:attributeList>
                    <ns2:name>private_ip_address</ns2:name>
                    <ns2:value>10.0.215.19</ns2:value>
                </ns2:attributeList>
                <ns2:attributeList>
                    <ns2:name>Code</ns2:name>
                    <ns2:value></ns2:value>
                </ns2:attributeList>
                <ns2:attributeList>
                    <ns2:name>uuid</ns2:name>
                    <ns2:value>eab5f52b-7225-4c51-9ca7-f26757a0d7f5</ns2:value>
                </ns2:attributeList>
                <ns2:attributeList>
                    <ns2:name>Notes</ns2:name>
                    <ns2:value></ns2:value>
                </ns2:attributeList>
                <ns2:beginDate>2018-01-24T16:15:00.018+02:00</ns2:beginDate>
                <ns2:className>cloudstack</ns2:className>
                <ns2:id>1542507</ns2:id>
            </ns2:cards>
            <ns2:totalRows>29</ns2:totalRows>
        </ns2:return>
    </ns2:getCardListResponse>
</soap:Body>

要动态更改应用的颜色,请参阅this

详细说明。

答案 1 :(得分:0)

有一种方法可以动态更改App的主题。

您需要提前在styles.xml中添加两个主题,如下所示:

<style name="AppThemeA" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">#aba424</item>
    <item name="colorPrimaryDark">#9f2020</item>
    <item name="colorAccent">#2a6c29</item>
</style>

<style name="AppThemeB" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">#706464</item>
    <item name="colorPrimaryDark">#831444</item>
    <item name="colorAccent">#183150</item>
</style>

在你的代码中使用其中一个。

例如,MainActivity中有按钮,如果点击它,您的主题将更改为AppThemeA,因此您需要在MainActivity {{1}中执行此操作}}:

OnCreate
相关问题