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

来源:互联网 发布:电子视频制作软件 编辑:程序博客网 时间:2024/06/04 19:45

之前由于项目中需要使用到沉浸式状态栏是没有将图片也沉浸的,而且单个的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地址

原创粉丝点击