[置顶] fragment中使用replace创建新的fragment遇到的层级错乱问题的一种解决方案
2017-08-01 13:05
399 查看
前些日子遇到了一个非常奇怪的情况,我的需求是在一个fragment内通过replace方法去创建一个新的fragment。但是,在这里出现了一个特别奇怪的问题:就是当我的replace方法执行成功之后,新的fragment的布局竟然在母fragment的一个Button的下方。
创建fragment的代码如下:
这是不应该的,因为明明我的新的fragment的布局应该在上方,为什么无法盖住母fragment的一个UI呢?在这里我已经把新的loginOrderMainFragment 的背景设置了颜色,但是却仍然无法遮住那个Button。
当我的母fragment的xml代码如下时:
这个id为login的Button的层级始终在fragment_my_order_lists这个framelayout派生的fragment之上。
而当代码改动如下时:
只是在Button的外侧套一个layout,这个时候,这个奇怪的问题就消失了。
如果遇到相似问题的朋友可以尝试一下我上面的方法,但是如果有更好的解决方案(也可能我犯了低级错误~),麻烦告知一下,谢谢~
创建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,这个时候,这个奇怪的问题就消失了。
如果遇到相似问题的朋友可以尝试一下我上面的方法,但是如果有更好的解决方案(也可能我犯了低级错误~),麻烦告知一下,谢谢~
相关文章推荐
- 使用ef6创建oracle数据库的实体模型遇到的问题及解决方案
- 关于ibatis使用中遇到的问题 和解决方案
- 使用htmlcontrol-for-symbian创建自定义list遇到的问题
- 使用Mencoder进行视频转换遇到的问题和相关解决方案
- 【Richie Zhu】android开发时使用Android模拟器经常遇到连不上、连一次掉一次等诸多问题的解决方案
- Ubuntu 8.10使用过程中遇到的问题及解决方案
- 在MyEclipse下创建工程时,遇到的中文乱码问题及解决方案
- asp.net使用office遇到的权限问题解决方案
- 使用Git 过程中遇到的几个问题及其解决方案
- 使用exslt str:replace template时遇到的问题
- [置顶] 使用mysql数据库开发过程当中遇到问题 -- 给关键字加 [关键字]的问题
- 使用Action创建一个JButton后,改变该按钮的名字可能会遇到意想不到的问题
- Fragment的使用与学习及使用过程中遇到的问题
- 使用VS2010时遇到的一些小问题的解决方案
- 使用Mencoder进行视频转换遇到的问题和相关解决方案
- “Visual Studio .NET 无法创建或打开应用程序。问题很可能是因为本地WEB本地服务器上没有安装所需的组件。请运行Visual Studio .NET安装程序并添加WEB开发组件“的一种解决方案
- “Visual Studio .NET 无法创建或打开应用程序。问题很可能是因为本地WEB本地服务器上没有安装所需的组件。请运行Visual Studio .NET安装程序并添加WEB开发组件“的一种解决方案
- 解决方案:使用OWC11成生统计图时遇到的问题
- 分享一个copyfile使用时遇到的问题及解决方案
- Appfuse使用中遇到的问题及解决方案