gpt4 book ai didi

java - 如何保存 View 组件的位置?

转载 作者:太空宇宙 更新时间:2023-11-04 11:52:35 24 4
gpt4 key购买 nike

我在 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);

关于java - 如何保存 View 组件的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41664356/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com