繁体   English   中英

如何保存视图组件的位置?

[英]How to save the position of views components?

我在LinearLayout中有10个frameLayouts。 在我的应用程序中,我可以移动这些帧,但如果我关闭它,我的帧的位置不会保存。 如果我再次打开应用程序,我会看到我的框架的默认位置? 我怎样才能保住位置? 救命!! PS在我的应用程序中,我使用sharedPreferences。

您可以使用共享首选项将内容存储在内存中并在以后恢复它们: 用于SharedPreferences的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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM