添加Fragment时如何显示底部按钮?不调整Fragment尺寸
解决Fragment跳转后底部按钮消失的问题
看起来你的问题出在布局层级或者Fragment跳转逻辑上,我帮你一步步分析和解决:
核心原因推测
从你的布局代码和截图来看,ID为tv_chat的按钮(显示数字234)是放在RelativeLayout容器内,位置在底部tabLayout上方30dp处。跳转新Fragment后按钮消失,大概率是新Fragment的布局覆盖了它,或者Fragment跳转时把包含该按钮的Home_Fragment给替换掉了。
具体解决方案
1. 调整按钮的布局层级(最直接的修复)
在RelativeLayout中,View的显示层级是按代码顺序决定的——越靠后的View,层级越高,越不容易被其他View覆盖。你当前的tv_chat定义在tabLayout之前,把它移到布局的最后面就能提升层级,避免被Fragment覆盖:
修改后的布局代码:
<RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/white"> <!-- View pager to swipe views --> <Button android:id="@+id/button3" android:layout_width="125dp" android:layout_height="59dp" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="0dp" android:background="@color/white" android:text="" /> <com.medyc.us.customViews.CustomViewPager android:id="@+id/viewpager" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_above="@+id/tabLayout" android:background="@color/light_background" /> <!-- our tablayout to display tabs --> <android.support.design.widget.TabLayout android:id="@+id/tabLayout" android:layout_width="match_parent" android:layout_height="50dp" android:layout_alignParentBottom="true" app:tabIndicatorHeight="0dp" /> <View android:id="@+id/bottomview" android:layout_width="match_parent" android:layout_height="43dp" android:layout_alignParentBottom="true" android:layout_alignParentStart="true" android:layout_marginBottom="50dp" /> <!-- 把tv_chat移到最后,提升显示层级 --> <Button android:id="@+id/tv_chat" android:layout_width="60dp" android:layout_height="60dp" android:background="@drawable/circle_blue_gradient" android:text="234" android:gravity="center" android:textSize="@dimen/twenty_sp" android:textColor="@color/white" android:textStyle="bold" android:visibility="visible" android:fontFamily="@font/helvetica" android:layout_centerHorizontal="true" android:layout_alignBottom="@+id/tabLayout" android:layout_marginBottom="@dimen/thirty_dp" /> </RelativeLayout>
2. 检查Fragment的跳转逻辑
如果调整层级没用,就要看你跳转Fragment的代码:
- 如果你用
replace()方法替换了Home_Fragment所在的容器,Home_Fragment会被直接移除,里面的tv_chat自然消失。这种情况改用add()方法添加新Fragment,并加入回退栈:
同时记得给新Fragment的根布局设置透明背景(getFragmentManager().beginTransaction() .add(R.id.your_container_id, new YourTargetFragment()) .addToBackStack(null) .commit();android:background="@android:color/transparent"),这样Home_Fragment的内容才能透出来。
3. 排查动态隐藏按钮的代码
检查跳转Fragment前后的业务逻辑,有没有不小心调用tv_chat.setVisibility(View.GONE)或View.INVISIBLE的操作,这也会导致按钮消失。
补充说明
你提到不想调整当前Fragment的尺寸,上面的方法都不需要修改Fragment的大小,只是调整布局层级或跳转方式,完全符合你的需求。
内容的提问来源于stack exchange,提问作者Rohit Shrivastava
相关产品推荐
相关产品推荐

