简体   繁体   English

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

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

I have 10 frameLayouts in LinearLayout. 我在LinearLayout中有10个frameLayouts。 In my app I can move these frames, but if I close it, position of my frames didn't save. 在我的应用程序中,我可以移动这些帧,但如果我关闭它,我的帧的位置不会保存。 And if I open app again I see default position of my frames? 如果我再次打开应用程序,我会看到我的框架的默认位置? How can I save position? 我怎样才能保住位置? Help!! 救命!! PS In my app I use sharedPreferences. PS在我的应用程序中,我使用sharedPreferences。

You can use Shared Preferences to store stuff in memory and restore them at later time: Android docs for SharedPreferences 您可以使用共享首选项将内容存储在内存中并在以后恢复它们: 用于SharedPreferences的Android文档

Example - Storing and restoring an integer called position: 示例 - 存储和恢复名为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