如何为版面设置不同的主题 [英] How to set a different theme to a Layout

查看:133
本文介绍了如何为版面设置不同的主题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为整个应用设置了默认主题.它在styles.xml中定义如下:

I've set a default theme for the whole app. It's defined in styles.xml as follows:

    <style name="DefaultTheme" parent="@android:style/Theme.Holo.Light">
        <!-- Customization here -->
    </style>

我还定义了一个黑暗的主题:

I've also defined a dark theme:

    <style name="DarkTheme" parent="@android:style/Theme.Holo">
        <!-- Customization here -->
    </style>

在清单中,声明轻主题为应用程序的主要主题:

In the manifest it is declared the light theme as the main theme for the app:

    <application
    ...
    android:theme="@style/DefaultTheme" >

现在这可以正常工作,但是在一个活动中,我需要为单个布局设置不同的主题.像这样:

Now this is working fine, but in an activity, I need to set a different theme for a single Layout. Something like this:

    +--------------------------------------------------+
    |         Parent Linear layout (default theme)     |
    |                                                  |
    | +------------------------------------+ +-------+ |
    | |                                    | |       | |
    | |     Left linear layout             | |       | |
    | |     (default theme)                | |       | |
    | |                                    | |       | |
    | |                                    | |       | |
    | |                                    | |    ·<----------- Right Linear Layout
    | |                                    | |       | |        (Need it in dark theme)
    | |                                    | |       | |
    | |                                    | |       | |
    | +------------------------------------+ +-------+ |
    +--------------------------------------------------+

在布局xml文件中,我正在尝试为最右边的子级LinearLayout设置主题:

In the layout xml file I'm trying to set a theme for the rightmost child LinearLayout:

    <LinearLayout
    style="@style/DarkTheme">
    ...

我希望它能很好地工作,并且只将深色主题应用于正确的布局(及其子视图),但它不起作用.我试图用内置的@android:style替换@style无济于事.我已经在布局编辑器和真实设备/模拟器中对此进行了测试.

I'd expect this to work just fine, and to apply the dark theme to the right layout only (and its child views), but it is not working. I've tried replacing the @style with a built-in @android:style to no avail. I've tested this in the layout editor and on real device/simulator.

是否可以将自定义主题或样式应用于单个布局?

Is it possible to apply a custom theme or style to a single layout?

推荐答案

现在可以通过在视图上使用android:theme属性并将其设置为您喜欢的任何主题来实现.请注意,子视图将继承其父主题.

This is now possible by using the android:theme property on the view and setting it to any theme you like. Note that the child views will inherit the theme of their parent.

这篇关于如何为版面设置不同的主题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆