根据主题更改布局背景

时间:2014-12-17 20:22:43

标签: android layout

我有以下要素:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/giftsLayout">

我的 style.xml 文件是

<style name="ChristmasTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowBackground">@drawable/christmas_background</item>
    <item name="android:textColor">#03426A</item>
</style>
<style name="BirthdayTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowBackground">@drawable/birthday_background</item>
    <item name="android:textColor">#03426A</item>
</style>

我需要依赖于ChristmasTheme或者BirthdayTheme应用程序主题将我的背景设置为我的LinearLayout或者更好地设置我的LinearLayout并使用id giftsLayout。我怎么能这样做?

2 个答案:

答案 0 :(得分:0)

基本上,你需要一种方法来改变主题/背景吗? 您必须在运行时以onCreate方法执行此操作。

您可以使用setTheme()方法定义条件(例如日期)或让用户选择何时切换..

答案 1 :(得分:0)

您需要将此添加到/ create attr.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="windowBackground" format="reference" />
</resources>

然后在你的视图中:

android:background="?attr/windowBackground"

然后Android会负责切换背景,假设您正确更改了主题;当然。

相关问题