实现用户对主题的选择 [英] Implementing user choice of theme

查看:20
本文介绍了实现用户对主题的选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让用户在几个不同的主题之间进行选择,并且想知道这是否是一种正确的做事方式.我用这个方法做了一点测试,确实有效,但我认为可能有更好的方法,并且认为它以后可能会导致一些问题,所以想问一下.

I want to give the user the choice between a few different themes, and was wondering if this is an alright way of doing things. I did a little test with this method and it worked, but I think there may be better ways and think it may cause some problems later on, so wanted to ask.

我想为每个主题创建一个不同的布局,而在 onCreate 中只是有一个 setContentView() 方法的开关.我会首先加载一个保存的 SharedPreference 值(整数),并根据该值显示相应的布局.显然,用户可以使用按钮或其他东西更改 SharedPreference 值.

I was thinking of creating a different layout for each theme, and in onCreate just having a switch for the setContentView() method. I'd load a saved SharedPreference value (integer) first and depending on what that value was display the corresponding layout. Obviously the user could change the SharedPreference value with a button or something.

由于这些布局基本相同但颜色不同,因此我想为每个布局文件中的 TextViews 和其他视图使用相同的 ID.我的主要问题是这会导致问题吗?

As these layouts would be basically the same but with different colours, I'd want to use the same IDs for my TextViews and other Views in each layout file. My main question is would this cause problems?

抱歉没有代码的文字墙.我只想大致了解这种情况下的良好做法.提前致谢.

Sorry for the wall of text with no code. I'd just like to get a general idea of good practice for this situation. Thanks in advance.

推荐答案

我的应用程序中实际上有这个功能,此外,我允许用户在运行时更改主题.由于从首选项中读取值需要一些时间,因此我通过保存缓存值的全局可访问函数获取主题 ID.

I actually have this feature in my application and additionally, I allow users to change theme at runtime. As reading a value from preferences takes some time, I'm getting a theme id via globally accessible function which holds cached value.

正如已经指出的 - 创建一些 Android 主题,使用 本指南.您的 styles.xml 文件中至少有两个