- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在销毁一个以编程方式创建的 fragment :
getFragmentManager().beginTransaction().remove(getFragmentManager().findFragmentById(R.id.test)).commit();
在xml文件中是这样决定的:
<LinearLayout
android:id="@+id/test"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</LinearLayout>
如果我在主 Activity 中再次从同一个类创建一个 fragment :
getSupportFragmentManager().beginTransaction()
.add(R.id.result_bar, testinstance)
.commit();
然后 onCreate 似乎不再被调用( fragment 只是空的)。我在这里做错了什么?谢谢。
最佳答案
FrameLayout
:根据 Google Documentation on Commons Layouts和这个What are the differences between LinearLayout, RelativeLayout, and AbsoluteLayout?的答案, ViewGroup
作为 LinearLayout
, RelativeLayout
, AbsoluteLayout
(已废弃),TableLayout
等可以显示 View :
FrameLayout
通过重叠显示 View 。它通常用于包含布局:
"Frame layouts are one of the simplest and most efficient types of layouts used by Android developers to organize view controls. They are used less often than some other layouts, simply because they are generally used to display only one view, or views which overlap. The frame layout is often used as a container layout, as it generally only has a single child view (often another layout, used to organize more than one view)."
"The Frame layout allows developers to display only a single or multiple UI elements within Frame Layout but each element will be positioned based on the top left of the screen, and elements that overlap will be displayed overlapping."
来源:Android Frame Layout For Absolute Beginner
Fragment
? (对比 LinearLayout 或
Google Documentation of FrameLayout说:
"FrameLayout is designed to block out an area on the screen to display a single item."
FrameLayout
将承载一个布局并且它愿意这样做。而其余的 ViewGroup
只是显示 View 。你可以做一个Fragment
总共ViewGroup
s (我对此进行了测试,这对我来说是一个惊喜) 但这不是执行此操作的正确方法。 FrameLayout
是:
"...the normal layout of choice when you want to overlap views."
如果您使用 <fragment .../>
创建布局,您的 fragment 将不会被另一个 fragment 替换,因为它已显示,并且在 View 上“附加”了它的 ID。要替换 fragment ,您需要托管它:“通过将 fragment 封装在 FrameLayout 中,您可以仅替换细节”(参见 this answer)。
然后,记住 FrameLayout
s 是空的,可以承载一个布局。 Fragment
s(Fragment Documentation from Google,它非常简单地解释了如何使用 fragment 的事实),当它们在 xml 中声明时,它们必须附加一个类(一个 id),不能被替换。
这就是为什么我们需要一个容器来承载这个 fragment 并重叠 Activity 的 View !
希望这会有所帮助。
注意:如果有人想编辑这个答案,因为它没有解释或解释得不好,她/他可以衷心同意。
关于java - Android: remove() Fragment--> add() new Fragment of same class again ->onCreateView and onActivityCreated 没有被调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20664090/
我知道它们是匿名类型,但我不明白 Razor 语法。在一些文档中,我找到了这样的示例: @Html.Label("Hello", new { htmlAtributes = new { id = "h
关于:new Object(new Array()) 有一个相当基本的问题,我自己确实无法给出答案,我正在寻求建议: 在js中实例化对象时使用如下方法: var obj = new Object();
在eclipse中右击项目时,“新建文件夹”、“新建源文件夹”和“新建包”有什么区别?他们似乎都在做同样的事情,引用文献并没有说太多。 谢谢 最佳答案 新建文件夹 在项目中创建一个新文件夹。 新建源文
几天来我一直在测试 bolt-cms,我试图了解它是如何工作的。 我想知道新页面、新条目和新展示柜之间有什么区别。 我已阅读 this它并没有填补空白。 最佳答案 Pages、Entries 和 Sh
更新:感谢所有的回答。我发现的最干净的解决方案是这个: if ( k(Arrays.asList(new LinkedList<>())); 我有一个递归方法,可以从列表中生成所有“n 选 k”组合。
我现在想知道这些指令是如何分配内存的。 例如,如果我得到代码怎么办: x = new int[5]; y = new int[5]; 如果分配了这些,它在 RAM 中的实际情况如何?是为每个变量保留整
我希望将其写入output.txt而不清除它 - 只是附加到末尾。但是,当我使用以下两种方法时: public void addEmails(ArrayList emails){ for (i
我正在分配内存,稍后将用于构造具有放置 new 的对象。我应该使用 operator new(n),还是应该使用 new unsigned char[n]?为什么? 最佳答案 因素: new[] 必须
基本上,我的问题是以下代码是否有效。 void* mem = operator new(sizeof(T)); T* instance = new(mem) T; delete instance; 如
很抱歉,如果之前有人问过这个问题,但我想就以下两种用法之间的区别提供一个简明的答案。 VS 似乎将它们都接受为有效代码。 private static void doSomeWork() { /
请告诉我这段代码在做什么,它是否创建多维数组(我认为不是)? 代码片段.. var hanoi_peg = new Array( new Array( 5, 4, 3, 2, 1,
这个问题在这里已经有了答案: String intern() behaviour (4 个答案) When should we use intern method of String on Stri
许多人说您应该避免使用 new Object、new Array(),而是使用 {}。 [] 和真/假。 使用字面量构造来获取对象或数组的新实例而不是使用 new 有什么好处?我知道 Crockfor
我正在开发一个存在内存泄漏的开源库。该库是围绕 boost::asio 构建的数据流服务。服务器端使用堆内存管理系统,该系统提供内存以容纳有限数量的 samples,同时它们等待通过 tcp 连接被推
我从以下函数中得到内存泄漏: int ReadWrite(int socket, char *readfile) { FILE *rf = NULL; rf = fopen(readfile,
在考虑类似的事情时 auto x = new T; 标准是否强制要求内存必须来自operator new——类特定的还是全局的?也就是说,如果缺少特定于类的 operator new,则没有办法从除全
只是出于好奇:为什么 C++ 选择 a = new A 而不是 a = A.new 作为实例化对象的方式?后者不是更像是面向对象的吗? 最佳答案 Just out of curiosity: Why
考虑以下代码: typedef SomeType type_t[2]; SomeType * arr1 = new type_t; //new or new[] ??? type_t * arr2
这个问题在这里已经有了答案: Difference between 'new operator' and 'operator new'? (8 个答案) 关闭 8 年前。 面试题:"new"运算符和
我正在为一个应用程序设计界面,以在 TableLayout 中显示从数据库中提取的一些数据。现在,默认 View 是纵向的,它由一个下拉菜单和一个三列的表格组成。当用户切换到横向时,微调器及其选项可以
我是一名优秀的程序员,十分优秀!