Android解决Fragment使用replace方法重叠问题
来源:互联网 发布:ping 用的是哪个端口 编辑:程序博客网 时间:2024/04/26 11:13
Android解决Fragment使用replace方法重叠问题
今天在使用Fragment进行replace切换的时候,发现居然没反应:
布局是这样的
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <fragment android:id="@+id/top" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:name="com.xieth.as.fragment_one.TopFragment" /> <LinearLayout android:id="@+id/center_layout" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="6" > <fragment android:id="@+id/center" android:name="com.xieth.as.fragment_one.CenterFragment" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout></LinearLayout>
切换代码是这样的
AnotherFragment anotherFragment = new AnotherFragment(); FragmentManager fragmentManager = getFragmentManager(); android.app.FragmentTransaction transaction = fragmentManager.beginTransaction(); transaction.replace(R.id.center_layout, anotherFragment); transaction.commit();
代码是没有问题的,其实问题就出现在LinearLayout,必须把LinearLayout改成FrameLayout,这样就有效果了,不过会有一个情况,就是重叠了。
解决方案
在相应的Fragment布局里面设置background即可
android:background="@android:color/background_light"
center.layout
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/background_light" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="使用Fragment做主面板" android:textColor="@android:color/background_dark" android:textSize="15sp" /></LinearLayout>
another.layout
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/background_light" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="Another Fragment" android:textColor="@android:color/background_dark" android:textSize="15sp" /></LinearLayout>
这样就行了
0 0
- Android解决Fragment使用replace方法重叠问题
- Android 、fragment、replace重叠问题
- 解决fragment replace 重叠现象
- Android 解决fragment replace方法低效的问题
- 解决Fragment重叠问题
- 解决 Fragment 重叠问题
- Android fragment 重叠问题
- android 使用fragment切换,出现fragment重叠的问题
- 解决viewPager fragment有时候内容重叠的问题的方法
- Android中管理多个Fragment,解决重叠的问题
- 使用Fragment代替ActivityGroup,并解决屏幕旋转重叠问题
- 关于Fragment 使用重叠问题。
- Fragment 重叠 遮盖问题 完美解决
- 关于Fragment重叠问题分析和解决
- 完美解决Fragment重叠的问题
- android组件 fragment 重叠现象解决思路
- Android fragment 重叠问题的解决方法
- Android fragment 重叠问题的解决方法
- Codeforces 14D Two Path Solution
- Multithreading and memory 读书笔记---_strong修饰词
- 【NOIP模拟】平均数
- ERROR ITMS-90164: "Invalid Code Signing Entitlements. The entitlements in your app bundle signature
- UIScrollView
- Android解决Fragment使用replace方法重叠问题
- Two Sum
- [Android] TopSheetBehavior
- PHP学习笔记-PHP与Web页面的交互2
- JavaScript 函数调用时带括号和不带括号的区别
- EditText+CheckBox使输入框的内容明文/密文显示
- spring依赖注入原理
- Hex Editor in Linux —— hte —— VIM
- echarts解决360及IE8兼容性问题