我在 LinearLayout 中有 10 个frameLayout。在我的应用程序中,我可以移动这些框架,但如果我关闭它,框架的位置不会保存。如果我再次打开应用程序,我会看到框架的默认位置? 如何保存位置?帮助!! 附:在我的应用程序中,我使用sharedPreferences。
请您参考如下方法:
您可以使用共享首选项将内容存储在内存中并在以后恢复它们: Android docs for SharedPreferences
示例 - 存储和恢复称为位置的整数:
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);