如何保存视图组件的位置



我在 LinearLayout 中有 10 个 frameLayout。在我的应用程序中,我可以移动这些框架,但是如果我关闭它,我的框架位置不会保存。如果我再次打开应用程序,我看到框架的默认位置?如何保存仓位?帮助!!附言:在我的应用程序中,我使用共享首选项。

您可以使用"共享偏好设置"将内容存储在内存中,并在以后恢复它们:适用于共享首选项的 Android 文档

示例 - 存储和恢复名为 position 的整数:

    int position = 0;
    //get the SharedPreferences for "yourContextName", which you need to replace it with you context name
    SharedPreferences pref = getSharedPreferences("yourContextName", Context.MODE_PRIVATE);
      /* --------------- Storing data -------------------- */
     //Get editor for writing data to memory
     SharedPreferences.Editor editor = pref.edit();
    //Add an integer to editor
    editor.putInt("position", position);
    //Must commit or else the data will NOT be stored
    editor.commit();

    /* ---------------- Retrievig data ------------------ */
    //Retrievig an integer from pref with tag "position" (-1 is the default value)
    int retInt = pref.getInt("position", -1);

最新更新