简体   繁体   English

如何使应用程序具有黑暗和明亮的主题?

[英]How can I make an app dark and light theme?

I need to make two themes for android app. 我需要为Android应用程序制作两个主题。 Dark and Light theme. 黑暗与光明的主题。 And I want to use custom colors, sizes, fonts... So I want to make something like: 我想使用自定义颜色,大小,字体...所以我想做些类似的事情:

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
            <!-- Customize your theme here. -->
            <!-- sizes, fonts... -->
    </style>

    <style name="AppTheme.Dark" parent="AppTheme">

    </style>

    <style name="AppTheme.Light" parent="AppTheme">

    </style>

and then I want to make Dark and Light theme to extend AppTheme and just to change colors. 然后我要制作“黑暗与光明”主题以扩展AppTheme并仅更改颜色。

So I need some tutorials, tips or whatever how to do this most efficiently. 所以我需要一些教程,技巧或最有效的方法。 I appreciate every advice, thank you. 我感谢所有建议,谢谢。 :) :)

I think you are on a good track, you would define different styles for each and every theme, something like this: 我认为您进展顺利,您将为每个主题定义不同的样式,如下所示:

<style name="AppTheme.Dark" parent="AppTheme">
        <item name="colorPrimary">/* color for dark theme */ </item>
        <item name="android:windowBackground">/* color for dark theme */ </item>
        // default font color for dark theme
        // etc.
</style>

<style name="AppTheme.Light" parent="AppTheme">
        <item name="colorPrimary">/* color for light theme */ </item>
        <item name="android:windowBackground">/* color for light theme */ </item>
        // default font color for light theme
        // etc.
</style>

AppTheme would define the style common to both Dark & Light themes. AppTheme将定义“黑暗与黑暗”主题的通用样式。

Also, you would have a setting in your app that keeps track of current user theme. 另外,您将在应用程序中有一个设置来跟踪当前用户主题。 So let's imagine that this setting is kept in SharedPreferences. 因此,让我们想象一下此设置保留在SharedPreferences中。 Then, every time an Activity is created, you would check the current setting and apply the corresponding theme: 然后,每次创建活动时,您都将检查当前设置并应用相应的主题:

@Override
protected void onCreate(Bundle savedInstanceState) {
    boolean darkModeIsEnabled = // read the setting from SharedPreferences

    if (darkModeIsEnabled) {
        setTheme(R.style.AppTheme_Dark)
    } else {
        setTheme(R.style.AppTheme_Light)
    }
    super.onCreate(savedInstanceState)
    setContentView(R.layout.my_activity)
}

One last point, you probably will want the users to switch the theme from the app, in that case you would need to recreate the activity/activities so that the new theme is applied. 最后一点,您可能希望用户从应用程序切换主题,在这种情况下,您将需要重新创建一个或多个活动,以便应用新主题。

For an example app you could have a look here . 对于示例应用程序,您可以在这里看看。 You can inspect the themes.xml file to see how there it's done. 您可以检查themes.xml文件以查看完成情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM