【问题标题】:How to reference a style in a custom theme如何在自定义主题中引用样式
【发布时间】:2012-06-15 17:32:56
【问题描述】:

我有一个登录屏幕,该屏幕针对我的应用程序的不同构建进行了不同的标记。我需要此屏幕的布局文件中的背景图像不同,因此我想为顶级容器指向不同的样式。我对如何做到这一点有点不知所措。

我已经声明了一个类似的样式:

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

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

【问题讨论】:

    标签: android themes android-styles


    【解决方案1】:

    好的,我想通了,样式参考应该是:

    style="?attr/loginPageContainerStyle"
    

    我想我会分享。

    【讨论】:

    • android 主题和样式的好例子。只需提一下,在 AndroidManifest 上的应用程序标签上需要添加 android:theme="@style/ThemeOne" 或 android:theme="@style/ThemeTwo" 这就是我们需要了解的所有主题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-04
    • 2020-01-17
    • 1970-01-01
    • 1970-01-01
    • 2011-05-28
    • 2016-04-09
    • 1970-01-01
    相关资源
    最近更新 更多