gpt4 book ai didi

android - 如何在 Android 中自动显示浅色和深色主题的不同资源?

转载 作者:行者123 更新时间:2023-11-29 14:09:41 26 4
gpt4 key购买 nike

据我了解,Android 有两个内置主题,Light 和 Dark。我希望我的应用程序根据用户使用的主题显示不同的资源(不是让用户在我的应用程序中明确指定主题!)。这可能吗?如果是这样,是否可以通过 xml 资源来完成,或者是否需要编程方法?

注意:是的,我读过 http://d.android.com/guide/topics/ui/themes.html至少三次。它似乎只告诉我如何为我的元素设置一个特定的主题或样式,而不是正确显示亮/暗元素。

最佳答案

是的,可以通过 XML 资源完成。

来自 https://developer.android.com/preview/features/darktheme#themes_and_styles :

Your themes and styles should avoid hard-coded colors or icons intended for use under a light theme. You should use theme attributes (preferred) or night-qualified resources instead.

所以是的,有资源限定符,请参阅 https://developer.android.com/guide/topics/resources/providing-resources.html#NightQualifier: nightnotnight 后缀。

我找到了 Material theme website很好地解释这一点,但最重要的部分是:

Alternatively, if you want to define separate Light and Dark themes for your app, you can inherit from Theme.MaterialComponents.Light in the values directory, and Theme.MaterialComponents in the values-night directory. E.g.:

res/values/themes.xml

<style name="Theme.MyApp" parent="Theme.MaterialComponents.Light">
<!-- ... -->
</style>

res/values-night/themes.xml

<style name="Theme.MyApp" parent="Theme.MaterialComponents">
<!-- ... -->
</style>

关于android - 如何在 Android 中自动显示浅色和深色主题的不同资源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4321675/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com