日/夜主题为Android应用 [英] Day/Night theme for android app

查看:111
本文介绍了日/夜主题为Android应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序中,我需要实现昼/夜的主题。不幸的是,只需使用样式做主题化没有简单的方法,我需要能够更新:布局背景,按钮选择器,文本颜色,文本大小,图片,图标,动画

I have an app in which I need to implement day / night theme. Unfortunately there is no simple way of making theming by just using styles, I need to be able to update: layout backgrounds, button selectors, text color, text size, images, icons, animations.

这是我所看到的我有两个选择:

From what I see I have 2 options:

  1. 有不同的XML布局文件夜/天,所以像 home_day.xml / home_night.xml 。大约有30屏幕的应用程序,所以最终将有60 XML布局。在活动/片段的onCreate,基于当前的小时我可以的setContentView 。这增加了一些XML文件,但避免了增加更多的code。在活动

  1. Have different xml layout files for night/day, so something like home_day.xml / home_night.xml. There are around 30 screens in the app, so in the end there will be 60 xml layouts. On activity/fragment onCreate, based on current hour I could setContentView. This adds some more xml files but avoids adding more code in activities

有只有一个昼/夜和布置作业的的onCreate findviewById因为我想主题,根据当前的昼/夜更新自己的属性,每一个项目。这会产生很多额外的code,findviews和应用属性很多意见。

Have only one layout for day/night and on activity's onCreate findviewById for each item I want to theme and update his attributes based on current day/night. This could generate a lot of extra code, findviews and apply attributes for many views.

我的目标为2,但我愿意接受你的任何建议。所以,你会选择,为什么?

I am aiming for for 2. but I am open to any suggestions from you. So, what would you choose and why ?

推荐答案

其实这似乎可以用主题来形容自绘区域为好。看看:<一href="http://stackoverflow.com/questions/13302317/how-to-switch-between-night-mode-and-day-mode-themes">How到夜晚模式和白天模式主题之间进行切换。您可以通过使用样式块创建你的主题,然后在你的XML布局中,可以通过使用指定的东西在你的主题?ATTR。那么你应该能够调用setTheme(R.styles.DAY_THEME)的下一个活动,一切都应该被更新。

Actually it seems you can use themes to describe custom drawables as well. Take a look at: How to switch between night-mode and day-mode themes. You create your themes by using a style block and then in your xml layout you specify something in your theme by using ?attr. Then you should be able to call setTheme(R.styles.DAY_THEME) on the next activity and everything should be updated.

这篇关于日/夜主题为Android应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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