fragment中使用replace创建新的fragment遇到的层级错乱问题的一种解决方案

前些日子遇到了一个非常奇怪的情况,我的需求是在一个fragment内通过replace方法去创建一个新的fragment。但是,在这里出现了一个特别奇怪的问题:就是当我的replace方法执行成功之后,新的fragment的布局竟然在母fragment的一个Button的下方
创建fragment的代码如下:

private void switchToLoginState() {
        loginOrderMainFragment = new LoginOrderMainFragment();
        FragmentManager manager = getChildFragmentManager();
        FragmentTransaction ft = manager.beginTransaction();
        ft.replace(R.id.fragment_my_order_lists, loginOrderMainFragment);
        ft.commitAllowingStateLoss();
    }

这是不应该的,因为明明我的新的fragment的布局应该在上方,为什么无法盖住母fragment的一个UI呢?在这里我已经把新的loginOrderMainFragment 的背景设置了颜色,但是却仍然无法遮住那个Button。
当我的母fragment的xml代码如下时:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/bg_white"
    android:orientation="vertical"
    >
        <ui.NavigateBar
            android:id="@+id/navigateBar"
            android:layout_width="match_parent"
            android:layout_height="@dimen/topbarheight"
            android:background="@color/yellow_bar"
            app:navigate_bar_center_title="投注记录"
            app:navigate_return_enable="false"
            />
        <Button
            android:layout_below="@id/navigateBar"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:text="登录"
            android:id="@+id/login"
            />
        <FrameLayout
            android:id="@+id/fragment_my_order_lists"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
</RelativeLayout>

这个id为login的Button的层级始终在fragment_my_order_lists这个framelayout派生的fragment之上。

而当代码改动如下时:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/bg_white"
    android:orientation="vertical"
    >
        <ui.NavigateBar
            android:id="@+id/navigateBar"
            android:layout_width="match_parent"
            android:layout_height="@dimen/topbarheight"
            android:background="@color/yellow_bar"
            app:navigate_bar_center_title="投注记录"
            app:navigate_return_enable="false"
            />
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_below="@id/navigateBar"
            >
                <Button
                    android:layout_width="match_parent"
                    android:layout_height="60dp"
                    android:text="登录"
                    android:id="@+id/login"
                    />
        </LinearLayout>
        <FrameLayout
            android:id="@+id/fragment_my_order_lists"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
</RelativeLayout>

只是在Button的外侧套一个layout,这个时候,这个奇怪的问题就消失了。

如果遇到相似问题的朋友可以尝试一下我上面的方法,但是如果有更好的解决方案(也可能我犯了低级错误~),麻烦告知一下,谢谢~

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值