一個Activity中多個Fragment實現沉浸式狀態欄

一個Activity中多個Fragment實現沉浸式狀態欄,之前由於項目中需要使用到沉浸式狀態欄是沒有將圖片也沉浸的,而且單個的activity是比較好處理的,現在由於需求的變動需要實現在一個Activity中多個Fragment沉浸式,並且其中一個要對圖片實現沉浸式。在查閱資料,以及自已測試的過程中最終實現瞭。先看效果圖:
這裡寫圖片描述

詳細步驟:

1.首先將整個項目樣式修改為NoActionBar


        @color/colorPrimary
        @color/colorPrimaryDark
        @color/colorAccent

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 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:




    
    
    
    


數據適配器:

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 fragmentList;

    public void setFragmentList(List 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)




    

    

    

(2).其他fragment頁面




    

    

最後,通過以上代碼可實現簡單的一個Activity中多個Fragment實現沉浸式狀態欄,如果在寫的過程中有問題的請大傢及時糾正,謝謝。

You May Also Like