在 Android 中实现暗黑模式需要您在应用中设置主题。 为了在应用中启用暗黑模式,您需要在应用的主题中设置以下属性:
<style name="AppTheme" parent="Theme.AppCompat.DayNight">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
您还需要在您的清单文件中的 application
标签内指定这个主题:
<application
android:theme="@style/AppTheme">
...
</application>
您还可以使用以下方法来编程方式在运行时切换暗黑模式:
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
要在应用中使用暗黑模式,您还需要使用支持库中的 androidx.appcompat.app.AppCompatDelegate
类,并在布局文件中使用支持库中的控件。
更多信息请参阅官方文档:https://developer.android.com/guide/topics/ui/look-and-feel/darktheme
希望这些信息能帮到您!