如何在自定义主题中引用样式?

15

我有一个登录界面,不同版本的应用程序具有不同的品牌标识。我需要在该屏幕的布局文件中将背景图像设置为不同的样式,因此我想指向顶级容器的不同样式。但我不太清楚如何实现。

我声明了一个类似于以下的可定义样式:

<resources>

    <declare-styleable name="ThemeBase">
       <attr name="loginPageContainerStyle" format="reference" />
    </declare-styleable>

</resources>

我有几个不同的应用程序主题,如下:

<resources>

    <style name="ThemeBase" parent="android:style/Theme.Light" />

    <style name="ThemeOne" parent="ThemeBase">
       <item name="loginPageContainerStyle">@style/loginPageContainerThemeOne</item>
    </style>

    <style name="ThemeTwo" parent="ThemeBase">
       <item name="loginPageContainerStyle">@style/loginPageContainerThemeTwo</item>
    </style>

</resources>

我定义了以下样式:

<resources>
    <style name="loginPageContainerThemeOne">
        <item name="android:background">@drawable/background_theme_one</item>
    </style>

    <style name="loginPageContainerThemeTwo">
        <item name="android:background">@drawable/background_theme_two</item>
    </style>
</resources>

最后,一个类似于以下内容的login.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/loginRoot"
    style= [ ? WHAT GOES HERE ? ]
    android:gravity="center_horizontal"
    android:orientation="horizontal">

    [ LAYOUT STUFF ... ]

</LinearLayout>

我做错了什么吗?这样可以吗?

1个回答

18

好的,我解决了,样式引用应该是:

style="?attr/loginPageContainerStyle"

我觉得我应该分享一下。


1
很好的例子,涉及到Android主题和样式。只需要在AndroidManifest文件中的application标签中添加android:theme="@style/ThemeOne"或android:theme="@style/ThemeTwo",这就是我们需要了解的关于主题的全部内容。 - Ragaisis

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接