Kako sačuvati i koristiti podešavanja teme u Android aplikaciji?
sobes.tech АИ
Одговор од АИ
Efikasni pristup uključuje korišćenje SharedPreferences za čuvanje odabrane teme i AppCompatDelegate.setDefaultNightMode() za njenu primenu.
-
Čuvanje teme: Koristite
SharedPreferencesza skladištenje identifikatora odabrane teme (npr., "light", "dark", "system").// Dobijanje SharedPreferences val sharedPrefs = getSharedPreferences("AppTheme", Context.MODE_PRIVATE) // Dobijanje urednika val editor = sharedPrefs.edit() // Primena teme sa ID-jem themeId editor.putString("current_theme", themeId) // Čuvanje promena editor.apply() -
Primena teme pri pokretanju: U glavnoj
ActivityiliApplicationklasi, pročitajte sačuvanu vrednost i primenite temu.// Čitanje sačuvane vrednosti teme val themeId = getSharedPreferences("AppTheme", Context.MODE_PRIVATE) .getString("current_theme", "system") // Podrazumevano 'system' // Primena teme 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) } -
Promena teme od strane korisnika: Omogućite korisnički interfejs (npr.,
RadioButtonu podešavanjima), gde korisnik može da izabere temu. Pri izboru, sačuvajte novi identifikator uSharedPreferencesi odmah primenite temu pozivomAppCompatDelegate.setDefaultNightMode()i, ako je potrebno, restartujteActivityza ispravno primenjivanje.// Pretpostavimo da je korisnik izabrao tamnu temu val sharedPrefs = getSharedPreferences("AppTheme", Context.MODE_PRIVATE) val editor = sharedPrefs.edit() editor.putString("current_theme", "dark") editor.apply() // Primena teme odmah AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES) // Ili recreate() za potpuno primenjivanje teme // recreate()Restartovanje
Activitypomoćurecreate()može biti potrebno da bi se svi View-ovi u trenutnojActivityispravno osvežili sa novom temom. -
Definisanje tema u
styles.xml: Definišite stilove za svetlu i tamnu temu ures/values/styles.xmlires/values-night/styles.xml, nasleđujući odTheme.AppCompat.<!-- res/values/styles.xml --> <style name="AppTheme.Light" parent="Theme.AppCompat.Light"> <!-- Definicije stilova za svetlu temu --> </style> <!-- res/values-night/styles.xml --> <style name="AppTheme.Dark" parent="Theme.AppCompat"> <!-- Definicije stilova za tamnu temu --> </style>U
AndroidManifest.xmlnavedite osnovnu temu aplikacije:<!-- AndroidManifest.xml --> <application android:theme="@style/AppTheme.Light" ...> <!-- ... Activities --> </application> -
Praćenje promena teme: U slučaju da se tema menja sistemski (npr., po rasporedu ili ručno od strane korisnika putem podešavanja telefona), možete koristiti
Configuration.uiModeuonConfigurationChanged()za reagovanje, aliAppCompatDelegateobično to automatski obrađuje pri korišćenjuMODE_NIGHT_FOLLOW_SYSTEM.@Override override fun onConfigurationChanged(newConfig: Configuration) { super.onConfigurationChanged(newConfig) // Možete proveriti novu temu, ako je potrebno // val currentNightMode = newConfig.uiMode and Configuration.UI_MODE_NIGHT_MASK // ... }
Korišćenje AppCompatDelegate je preporučeni pristup za podršku tema na Androidu, jer obezbeđuje kompatibilnost unazad i pravilno funkcionisanje sa različitim komponentama biblioteke za podršku.