您的位置:首页 > 其它

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

2017-08-01 13:05 399 查看
前些日子遇到了一个非常奇怪的情况,我的需求是在一个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,这个时候,这个奇怪的问题就消失了。

如果遇到相似问题的朋友可以尝试一下我上面的方法,但是如果有更好的解决方案(也可能我犯了低级错误~),麻烦告知一下,谢谢~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐