英文:
How to change theme attributes dynamically?
问题
在我的应用程序中,我有多个用户帐户。每个用户可以定义自己的一组颜色,例如 colorPrimary
、colorPrimaryDark
、colorAccent
等。因此,每当用户登录时,活动将使用用户定义的颜色作为主题。
为了更好地理解,假设我的 user
如下所示:
class User {
String user_name = "Iron Man";
int colorPrimary = Color.RED;
int colorAccent = Color.BLUE;
...
注意:用户可以选择任何颜色。因此,没有预定义的主题。
是否可以完全动态地仅通过代码应用主题属性呢?
英文:
In my app, I have multiple user accounts. Each user can define their own set of colors. Such as colorPrimary
, colorPrimaryDark
, colorAccent
etc. So whenever a user get logged in, the activity will use the user defined colors
for theme
.
For better understanding, say my user
is:
class User {
String user_name = "Iron Man";
int colorPrimary = Color.RED;
int colorAccent = Color.BLUE;
...
Note: User can choose any color. So there will be no predefined themes.
Is it possible to apply theme attributes totally dynamically and only from the code?
答案1
得分: 2
我已在我的两个应用中创造了相同的想法,你有两个选择。
第一个选项,可以在你的theme.xml文件中创建许多主题,并根据用户使用activity的setTheme函数来更改整个主题,例如暗黑主题、白色主题、蓝色主题等,就像Askfm应用程序一样。
查看这个应用的代码,我在其中创建了这个功能。
https://github.com/amrdeveloper/askme
第二个选项,给予用户使用颜色选择器来改变一切的能力,并使用一些函数如getSupportActionBar、setStatusBarColor、setNavigationBarColor以及SharedPreferences来改变主题。
我已在我的应用中实现了这个想法,你可以从链接中查看结果。
https://play.google.com/store/apps/details?id=com.amrdeveloper.materialtimer
但是你不能在运行时编辑主题XML因为它是不可变的。
英文:
I have created the same idea in my two apps, you have two options
The first option, create many themes in your theme.xml file and change the full theme depend on the user using the setTheme function from activity, for example, dark theme, white theme, blue theme...etc like Askfm App
Check this app code, I have created this feature on it
https://github.com/amrdeveloper/askme
The second option, give the user the ability to change everything using a color picker
and use some functions like getSupportActionBar, setStatusBarColor, setNavigationBarColor, and SharedPreferences to change theme
I have implemented this idea on my app you can check the result from the link
https://play.google.com/store/apps/details?id=com.amrdeveloper.materialtimer
but you can't edit theme XML from the runtime because it immutable
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论