layout - 如何正确地将多个片段添加到片段过渡?

标签 layout view transactions fragment

我最近问了一个关于片段的问题here :

经过一番折腾后,我发现了问题所在,但经过更多的折腾和研究(其中正确的代码似乎与我的相同),我无法弄清楚我的问题是什么。

创建完所有内容后,我发现只有添加到事务中的最后一个片段可见。这是我添加它们的代码:

FragmentManager manager = getFragmentManager();
FragmentTransaction trans = manager.beginTransaction();
UrlListFragment urlfragment = new UrlListFragment();
MyWebFragment webfragment = new MyWebFragment();
trans.add(R.id.fragment_container, urlfragment, "my_url_fragment");
trans.add(R.id.fragment_container, webfragment, "my_web_fragment");
trans.commit();

这是我的主要 xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </FrameLayout>
</LinearLayout>

我做错了什么,或者可以做什么才能正确添加两个片段并可以正确看到?

最佳答案

不幸的是,您不能以这种方式使用片段,您只需每个容器使用一个片段。无论如何,它都会达到相同的视觉布局。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout
    android:id="@+id/fragment_container1"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</FrameLayout>
<FrameLayout
    android:id="@+id/fragment_container2"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</FrameLayout>
</LinearLayout>

FragmentManager manager = getFragmentManager();
FragmentTransaction trans = manager.beginTransaction();
UrlListFragment urlfragment = new UrlListFragment();
MyWebFragment webfragment = new MyWebFragment();
trans.add(R.id.fragment_container1, urlfragment, "my_url_fragment");
trans.add(R.id.fragment_container2, webfragment, "my_web_fragment");
trans.commit();

关于layout - 如何正确地将多个片段添加到片段过渡?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12444205/

相关文章:

安卓.view.InflateException : Binary XML file line #33: Error inflating class

tsql - T-SQL VIEW - CTE + UNPIVOT 对比 UNION 对比其他技术

c# - EF6 中的嵌套事务行为

java - 事务传播和持久性 与 Oracle 存储过程的上下文同步

java - Spring 数据 : Method with annotation @Query doesn't work as expected

CSS 内容 div 被推到菜单 div 下面

html - 灵活的布局以覆盖页面的其余部分并将高度分成两半供 child 使用

javascript - 在 Ruby on Rails 的 View 中加载 JavaScript

html - 制作水平搜索表单填充列空间 Bootstrap

Android:layout_width 和 height 裁剪我的图像