Kā saglabāt un izmantot motīva iestatījumus Android lietotnē?
sobes.tech AI
Atbilde no AI
Efektīva pieeja ietver SharedPreferences izmantošanu izvēlētās tēmas saglabāšanai un AppCompatDelegate.setDefaultNightMode() piemērošanai:
-
Tēmas saglabāšana: Izmantojiet
SharedPreferences, lai saglabātu izvēlētās tēmas identifikatoru (piemēram, "light", "dark", "system").// Iegūstam SharedPreferences val sharedPrefs = getSharedPreferences("AppTheme", Context.MODE_PRIVATE) // Iegūstam redaktoru val editor = sharedPrefs.edit() // Pielietojam tēmu ar ID themeId editor.putString("current_theme", themeId) // Saglabājam izmaiņas editor.apply() -
Tēmas piemērošana palaišanas laikā: Galvenajā
ActivityvaiApplicationklasē nolasiet saglabāto vērtību noSharedPreferencesun piemērojiet tēmu:// Nolasām saglabāto tēmas vērtību val themeId = getSharedPreferences("AppTheme", Context.MODE_PRIVATE) .getString("current_theme", "system") // Noklusējums 'system' // Pielietojam tēmu when (themeId) { "light" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO) "dark" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES) "system" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM) else -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM) } -
Lietotāja veiktās tēmas maiņa: Nodrošiniet lietotāja saskarni (piemēram,
RadioButtoniestatījumos), kur lietotājs var izvēlēties tēmu. Izvēloties, saglabājiet jauno identifikatoruSharedPreferencesun nekavējoties piemērojiet tēmu, izsaucotAppCompatDelegate.setDefaultNightMode()un, ja nepieciešams, pārstartietActivity, lai pareizi piemērotu tēmu:// Pieņemsim, lietotājs izvēlējās tumšo tēmu val sharedPrefs = getSharedPreferences("AppTheme", Context.MODE_PRIVATE) val editor = sharedPrefs.edit() editor.putString("current_theme", "dark") editor.apply() // Piemēro tēmu nekavējoties AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES) // Vai `recreate()` pilnīgai piemērošanai // recreate()Activitypārstartēšana arrecreate()var būt nepieciešama, lai visi skati pašreizējāActivitybūtu pareizi pārkrāsoti ar jauno tēmu. -
Tēmu definēšana
styles.xml: Definējiet gaišas un tumšas tēmas stilusres/values/styles.xmlunres/values-night/styles.xmlatbilstoši, mantojot noTheme.AppCompat:<!-- res/values/styles.xml --> <style name="AppTheme.Light" parent="Theme.AppCompat.Light"> <!-- Gaismas tēmas stilu definīcijas --> </style> <!-- res/values-night/styles.xml --> <style name="AppTheme.Dark" parent="Theme.AppCompat"> <!-- Tumšas tēmas stilu definīcijas --> </style>AndroidManifest.xmlnorādiet galveno lietojumprogrammas tēmu:<!-- AndroidManifest.xml --> <application android:theme="@style/AppTheme.Light" ...> <!-- ... Activities --> </application> -
Tēmas izmaiņu uzraudzība: Ja sistēma maina tēmu (piemēram, pēc grafika vai lietotāja manuāli), var izmantot
Configuration.uiModeonConfigurationChanged()reakcijai, betAppCompatDelegateparasti to automātiski apstrādā, izmantojotMODE_NIGHT_FOLLOW_SYSTEM.@Override override fun onConfigurationChanged(newConfig: Configuration) { super.onConfigurationChanged(newConfig) // Var pārbaudīt jauno tēmu, ja nepieciešams // val currentNightMode = newConfig.uiMode and Configuration.UI_MODE_NIGHT_MASK // ... }
AppCompatDelegate izmantošana ir ieteicamā pieeja tēmām Android, jo tā nodrošina atbalstu atpakaļ saderībai un pareizu darbību ar dažādām bibliotēkas komponentēm.