一个Activity中多个Fragment实现沉浸式状态栏

之前由于项目中需要使用到沉浸式状态栏是没有将图片也沉浸的,而且单个的activity是比较好处理的,现在由于需求的变动需要实现在一个Activity中多个Fragment沉浸式,并且其中一个要对图片实现沉浸式。在查阅资料,以及自已测试的过程中最终实现了。先看效果图:
这里写图片描述

详细步骤:

1.首先将整个项目样式修改为NoActionBar

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

2.在MainActivity中进行沉浸式兼容,因为沉浸式是只有在4.4以上才能实现的,我这里封装了一个工具类,调用的是setImmersionStateMode方法,需要在setContentView之前调用:

package com.fssmw.fm.statefragment.widget;

import android.app.Activity;
import android.graphics.Color;
import android.os.Build;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @date: 2017/7/12
 * @autror: LiFei
 * @description:  适配状态栏 MIUI Flyme android6.0+
 */
public class FitStateUI {

    /**
     * 兼容状态栏透明(沉浸式)
     * @param activity
     */
    public static void setImmersionStateMode(Activity activity){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT != Build.VERSION_CODES.LOLLIPOP) {
            // 透明状态栏
            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            // 透明导航栏
            // getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        } else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP) {
            Window window = activity.getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS |
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    // | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
            window.setNavigationBarColor(Color.TRANSPARENT);
        }
    }



    /**
     * 状态栏颜色字体(白底黑字)修改 MIUI6+
     * @param activity
     * @param darkmode
     * @return
     */
    public static boolean setMiuiStatusBarDarkMode(Activity activity, boolean darkmode) {
        Class<? extends Window> clazz = activity.getWindow().getClass();
        try {
            int darkModeFlag = 0;
            Class<?> layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
            Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
            darkModeFlag = field.getInt(layoutParams);
            Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class);
            extraFlagField.invoke(activity.getWindow(), darkmode ? darkModeFlag : 0, darkModeFlag);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 状态栏颜色字体(白底黑字)修改 Flyme4+
     * @param activity
     * @param dark
     * @return
     */
    public static boolean setMeizuStatusBarDarkIcon(Activity activity, boolean dark) {
        boolean result = false;
        if (activity != null) {
            try {
                WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
                Field darkFlag = WindowManager.LayoutParams.class
                        .getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON");
                Field meizuFlags = WindowManager.LayoutParams.class
                        .getDeclaredField("meizuFlags");
                darkFlag.setAccessible(true);
                meizuFlags.setAccessible(true);
                int bit = darkFlag.getInt(null);
                int value = meizuFlags.getInt(lp);
                if (dark) {
                    value |= bit;
                } else {
                    value &= ~bit;
                }
                meizuFlags.setInt(lp, value);
                activity.getWindow().setAttributes(lp);
                result = true;
            } catch (Exception e) {
            }
        }
        return result;
    }
}

3.主页tab切换我这里使用了ViewPager而不是FrameLayout,因为这里我也不清楚什么原因,状态栏怎么调试都不会透明。
activity_main:

<?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:orientation="vertical"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <!--CustomViewPager 禁止滑动-->
    <com.fssmw.fm.statefragment.widget.CustomViewPager  
        android:id="@+id/tab_vp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/background"
        android:layout_weight="1"/>
    <View
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:background="#c8c7cc"/>
    <android.support.design.widget.TabLayout
        android:id="@+id/tab"
        android:layout_width="match_parent"
        android:layout_height="49dp"
        app:tabBackground="@color/white"
        app:tabIndicatorColor="@color/app_blue"
        app:tabIndicatorHeight="0dp"
        app:tabMinWidth="64dp"
        app:tabMode="fixed"
        app:tabPadding="0dp"
        app:tabSelectedTextColor="@color/app_blue"
        app:tabTextColor="@color/app_dark_gray" />

</LinearLayout>

数据适配器:

package com.fssmw.fm.statefragment;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;

import java.util.List;

/**
 * Created by admin on 2017/7/24.
 */

public class FragPagerAdapter extends android.support.v4.app.FragmentPagerAdapter {
    private List<Fragment> fragmentList;

    public void setFragmentList(List<Fragment> fragmentList) {
        if (this.fragmentList != null) {
            this.fragmentList.clear();
        }
        this.fragmentList = fragmentList;
    }

    public FragPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        return fragmentList.get(position);
    }

    @Override
    public int getCount() {
        return fragmentList.size();
    }
}

4.对单独的每个Fragment处理,其实就是在自已的每个Xml布局中完成标题栏和图片
(1).图片显示(fragment_home)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <ImageView
        android:id="@+id/iv"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:src="@mipmap/ic_home_bg"
        android:scaleType="fitXY"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="64dp"
        android:gravity="bottom|center_horizontal"
        android:paddingBottom="10dp"
        android:text="首页"
        android:textColor="#000000"
        android:textSize="17sp"
        android:textStyle="bold"/>

    <TextView
        android:layout_below="@+id/iv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="22sp"
        android:textColor="#ff0000"
        android:gravity="center"
        android:text="首页"/>

</RelativeLayout>

(2).其他fragment页面

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="64dp"
        android:background="@color/colorPrimary"
        android:gravity="bottom|center_horizontal"
        android:paddingBottom="10dp"
        android:text="接单"
        android:textColor="#000000"
        android:textSize="17sp"
        android:textStyle="bold"/>

    <TextView
        android:layout_below="@+id/iv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="22sp"
        android:textColor="#00ff00"
        android:gravity="center"
        android:text="接单"/>

</LinearLayout>

最后,通过以上代码可实现简单的一个Activity中多个Fragment实现沉浸式状态栏,如果在写的过程中有问题的请大家及时纠正,谢谢。

Demo地址

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
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时需要使用不同的事务。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值