[英]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.