簡體   English   中英

在 onDestroy 之后保存更改的屬性

[英]Saving changed attributes after onDestroy

一般來說,我在我的應用程序中實現了樣式更改,但是在onDestroy()之后請求了應用程序樣式,我不知道如何保存它。

關閉應用程序后如何保存選定的應用程序主題。

主題更改如下,通過單擊themes.xml文件,應用某些屬性。

<resources xmlns:tools="http://schemas.android.com/tools">
    <style name="Theme.BackStackAndroidX" parent="Theme.MaterialComponents.DayNight.NoActionBar">
        <item name="colorPrimary">@color/lt_color_main</item>
        <item name="colorPrimaryVariant">@color/color_empty</item>
        <item name="colorOnPrimary">@color/color_empty</item>
        <item name="colorSecondary">@color/color_empty</item>
        <item name="colorSecondaryVariant">@color/color_empty</item>
        <item name="colorOnSecondary">@color/color_empty</item>
        <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
        <item name="android:windowTranslucentStatus">true</item>
        <item name="actionBarStyle">@style/MyActionBar</item>

        <item name="ColorStatusBar">@color/lt_color_appbar</item>
        <item name="ColorAppBar">@color/lt_color_appbar</item>
    </style>

    <style name="MyActionBar" parent="Widget.AppCompat.Toolbar">
        <item name="contentInsetStart">0dp</item>
    </style>

    <style name="Theme.BackStackAndroidX.LIGHT" parent="Theme.BackStackAndroidX">
        <item name="ColorStatusBar">@color/lt_color_appbar</item>
        <item name="ColorAppBar">@color/lt_color_appbar</item>
    </style>

    <style name="Theme.BackStackAndroidX.DARK" parent="Theme.BackStackAndroidX">
        <item name="ColorStatusBar">@color/dt_color_appbar</item>
        <item name="ColorAppBar">@color/dt_color_appbar</item>
    </style>

    <style name="Theme.BackStackAndroidX.MULTICOLOR" parent="Theme.BackStackAndroidX">
        <item name="ColorStatusBar">@color/mt_color_appbar</item>
        <item name="ColorAppBar">@color/mt_color_appbar</item>
    </style>

    <style name="TabBar">
        <item name="android:textSize">24sp</item>
    </style>
</resources>

按下時會出現以下情況

    public static int getThemeId(int theme) {
        int themeId = 0;
        switch (theme) {
            case 0: {
                themeId = R.style.Theme_BackStackAndroidX_LIGHT;
                break;
            }
            case 1: {
                themeId = R.style.Theme_BackStackAndroidX_DARK;
                break;
            }
            case 2: {
                themeId = R.style.Theme_BackStackAndroidX_MULTICOLOR;
                break;
            }
        }
        return themeId;
    }

您正在尋找共享的偏好。 這是為您提供的官方 Android 網站: https://developer.android.com/training/data-storage/shared-preferences

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM