android - 隐藏 fragment 留下空白

标签 android android-fragments fragmenttransaction fragmentmanager

我有一个 LinearLayout,它有 3 个容器(也是 LinearLayouts),并且它们有 weight=1。这是布局:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:baselineAligned="false"
    android:divider="?android:dividerHorizontal"
    android:orientation="horizontal"
    android:showDividers="middle" >

    <LinearLayout
        android:id="@+id/container1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#FF0000"
        android:orientation="vertical" >
    </LinearLayout>

    <LinearLayout
        android:id="@+id/container2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#00FF00"
        android:orientation="vertical" >
    </LinearLayout>

    <LinearLayout
        android:id="@+id/container3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical" >
    </LinearLayout>
</LinearLayout>

我在每个容器中添加了 1 个 fragment :

FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(R.id.container1, fragment1, TAG1);
transaction.add(R.id.container2, fragment2, TAG2);
transaction.add(R.id.container3, fragment3, TAG3);
transaction.commit();

所以现在他们的位置是这样的:

-------------------------------------
|           |           |           |
|           |           |           |
|           |           |           |
|           |           |           |
|           |           |           |
| fragment1 | fragment2 | fragment3 |
|           |           |           |
|           |           |           |
|           |           |           |
|           |           |           |
|           |           |           |
-------------------------------------

当我点击一个按钮时,我想首先隐藏 fragment 及其容器,并显示位于 fragment3 右侧的新 fragment 。所以我会有这样的事情:

-------------------------------------
|           |                       |
|           |                       |
|           |                       |
|           |                       |
|           |                       |
| fragment3 |       fragment4       |
|           |                       |
|           |                       |
|           |                       |
|           |                       |
|           |                       |
-------------------------------------

当我点击按钮时,我用它来隐藏 fragment :

FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.hide(fragment1);
transaction.hide(fragment2);
transaction.addToBackStack(null);
transaction.commit();

它将 fragment 及其容器隐藏在一起,但我得到的屏幕如下所示:

-------------------------------------
|           |           |           |
|           |           |           |
|           |           |           |
|           |           |           |
|           |           |           |
|   empty   |   empty   | fragment3 |
|           |           |           |
|           |           |           |
|           |           |           |
|           |           |           |
|           |           |           |
-------------------------------------

这里的 empty 意味着完全是空的,没有 fragment ,没有容器,什么都没有,只有空的空间。

所以,我的问题是如何在不留空白的情况下隐藏 fragment ?

最佳答案

我没有设法让它以这种方式工作,所以这是我的解决方案:

  • 我在布局中使用了 wrap_content 而不是 weight
  • 当我添加我的 fragment 时,我得到我的屏幕宽度并将每个 fragment 容器的宽度设置为整个宽度的 1/3。这样我得到的效果与 weight=1 相同。
  • 我没有隐藏和显示 fragment ,而是执行了以下操作:
    • 我将第 4 个 fragment 添加到第 3 个 fragment 的右侧。
    • 我添加了水平 ScrollView ,以便用户可以左右滚动并查看所有 fragment 。

关于android - 隐藏 fragment 留下空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19513557/

相关文章:

android - layout.xml 文本编辑器在 Eclipse android 中单击或突出显示时发生变化

java - RadioGroup 导致 Android 应用程序崩溃?

android - ListView 的标题 View 在配置更改时销毁

android - 如何从 fragment 验证 recyclerview 适配器 TextInputEditText?

Android 白色预览屏幕根据应用程序的启动位置花费不同的时间

Android 选项卡内容以获得更好的用户体验

android - fragment 与其他 fragment 重叠

android.R.id.content 作为 Fragment 的容器

android - 在 FragmentTransaction 之后调用什么方法

android - Windows 上的跨平台开发 (iOS/Android/Blackberry)