Activity嵌套多个Fragment实现不同全屏模式状态栏

效果图:
Fragment-A


Fragment-B

前提:如果采用了Appbarlayout 会产生阴影,要保存阴影;那么目前最好的解决方式就是通过代码动态设置填充fitSystemWindows属性,兼容4.4。
因为如果在xml里面设置了fitSystemWindows属性会导致Fragment的状态栏无法全屏。大概的原理是只能单独设置某一个Fragment的此属性,如果全部设置此属性的话会导致某些带有图片的全屏状态栏失效。
/**
**Fragment中重写该方法
**/
@Override
public void onHiddenChanged(boolean hidden) {
    System.out.println("FindFragment==>" + hidden);
    if (rView != null) {

        if (hidden) {
            mFfLlRoot.setFitsSystemWindows(false);
        } else {
            mFfLlRoot.setFitsSystemWindows(true);
        }
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {
            rView.requestApplyInsets();
        } else {
            rView.requestFitSystemWindows();
        }
    }
}



  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Activity中显示多个Fragment,可以通过以下步骤实现: 1. 定义多个Fragment类,实现onCreateView方法,返回Fragment的布局。 ```java public class MyFragment1 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_my1, container, false); return view; } } public class MyFragment2 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_my2, container, false); return view; } } ``` 2. 在Activity的布局中添加多个Fragment的容器。 ```xml <LinearLayout android:id="@+id/fragment_container1" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"/> <LinearLayout android:id="@+id/fragment_container2" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"/> ``` 3. 在Activity中通过FragmentManager动态添加多个Fragment。 ```java public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 获取FragmentManager FragmentManager fragmentManager = getSupportFragmentManager(); // 开始Fragment事务1 FragmentTransaction transaction1 = fragmentManager.beginTransaction(); // 添加Fragment1到容器1中 MyFragment1 fragment1 = new MyFragment1(); transaction1.add(R.id.fragment_container1, fragment1); // 提交事务1 transaction1.commit(); // 开始Fragment事务2 FragmentTransaction transaction2 = fragmentManager.beginTransaction(); // 添加Fragment2到容器2中 MyFragment2 fragment2 = new MyFragment2(); transaction2.add(R.id.fragment_container2, fragment2); // 提交事务2 transaction2.commit(); } } ``` 通过以上代码,即可在Activity中动态显示多个Fragment的UI界面。需要注意的是,每个Fragment需要对应一个独立的容器,并且在添加Fragment时需要使用不同的事务。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值