fragment中使用replace创建新的fragment遇到的层级错乱问题的一种解决方案
来源:互联网 发布:52单片机引脚图 编辑:程序博客网 时间:2024/05/18 17:45
前些日子遇到了一个非常奇怪的情况,我的需求是在一个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,这个时候,这个奇怪的问题就消失了。
如果遇到相似问题的朋友可以尝试一下我上面的方法,但是如果有更好的解决方案(也可能我犯了低级错误~),麻烦告知一下,谢谢~
阅读全文
1 0
- fragment中使用replace创建新的fragment遇到的层级错乱问题的一种解决方案
- 在Fragment中使用StartActivityForResult,RequestCode错乱的问题
- Fragment replace的使用
- Android中使用Fragment遇到的问题
- 使用多fragment出现的错乱或不显示问题
- Fragment的懒加载(解决ViewPager中Fragment生命周期错乱的问题)
- 使用Intent在Activity与Fragment之间传递数据中存在的返回键错乱问题
- Fragment的使用与学习过程中遇到的问题
- 使用Fragment+ViewPager中遇到的显示空白的问题
- 关于Fragment使用中遇到的一系列的问题
- 关于fragment的replace
- Fragment嵌套Fragment遇到的问题
- Android Fragment使用中遇到的几点问题
- 使用Fragment +searchview过程中遇到的问题
- CoordinatorLayout+TabLayout在Fragment中使用遇到的问题
- android Fragment中遇到的问题
- 使用Fragment时,遇到的问题
- Fragment使用中遇到的异常
- A1027
- 求 TOPK 的三种方法及分析
- 【LeetCode】611.Valid Triangle Number解题报告
- ios相关的一些知识
- selenium+python自动化测试(二)--使用webdriver操作浏览器
- fragment中使用replace创建新的fragment遇到的层级错乱问题的一种解决方案
- 设计一个针对全球的、访问量极大的id生成系统。 必须保证用户每次从该系统得到的id是唯一的,而且在概率上毫无碰撞可能。
- c语言的内存管理
- 应用经验 项目 基于51的数据采集系统感想(科技)
- L
- 53. Maximum Subarray
- JFinal后台权限项目启动时自动生成
- css的背景及背景图片的属性
- C++学习之路(2)---两招让你成为牛X的T型人才