您的位置:首页 > 移动开发 > Android开发

Android 沉浸式状态栏

2016-01-23 16:25 519 查看
extends:
https://www.zhihu.com/question/22102139 ,
http://www.2cto.com/kf/201504/395290.html ,
http://blog.csdn.net/u010255127/article/details/49308775 ,
http://www.bubuko.com/infodetail-1004629.html ,
http://www.bubuko.com/infodetail-1004629.html ,
http://blog.csdn.net/u010255127/article/details/49308775, http://tiku.io/questions/177218/keyboard-hiding-edittext-when-androidwindowtranslucentstatus-true
在Android4.4设备上支持沉浸式状态栏,只需要添加values-v19/styles.xml 下添加

<style name="AppBaseTheme" parent="@style/Theme.AppCompat.Light.NoActionBar" type="text/css"><item name="android:windowTranslucentNavigation">false</item>
<item name="android:windowTranslucentStatus">true</item>
</style>


亦或是通过JAVA代码实现,不过通过代码实现的时候要在每次Activity onCreate 周期里实现一下代码

          

public static void setImmerseLayout(View topBgView, Activity act) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//设置为沉浸式
act.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

//如果是MIUI主体,则设置为黑色的通知字体, 因为MIUI没有遮罩
MIUIFlymeUtils.setStatusBarTextColor(act);
MIUIFlymeUtils.setStatusBarDarkIcon(act.getWindow(), true);
//状态栏高度
int statusBarHeight = IUtil.getStatusHeight();
ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) topBgView.getLayoutParams();
params.height = statusBarHeight;
topBgView.setLayoutParams(params);

}
}


然后下第三方定制的UI中会出现白底白字的情况,例如MIUI和Flyme系统中

import android.app.Activity;
import android.os.Build;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;

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

//著作权归作者所有。
//        商业转载请联系作者获得授权,非商业转载请注明出处。
//        作者:Mariotaku
//        链接:https://www.zhihu.com/question/22102139/answer/24834510
//        来源:知乎
//// 检测MIUI
// 检测Flyme

public final class MIUIFlymeUtils {

private static final String KEY_MIUI_VERSION_CODE = "ro.miui.ui.version.code";
private static final String KEY_MIUI_VERSION_NAME = "ro.miui.ui.version.name";
private static final String KEY_MIUI_INTERNAL_STORAGE = "ro.miui.internal.storage";

/*
* 只支持MIUI V6
* @param context
* @param type 0--只需要状态栏透明 1-状态栏透明且黑色字体 2-清除黑色字体
*/
public static void setStatusBarTextColor(Activity context) {
int type = 1;
if (!isMIUI()) {
Log.d("ActionBar", "isMiUIV6:" + false);
return;
}
Log.d("ActionBar", "isMiUIV6:" + true);
Window window = context.getWindow();
Class clazz = window.getClass();
try {
int tranceFlag = 0;
int darkModeFlag = 0;
Class layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_TRANSPARENT");
tranceFlag = field.getInt(layoutParams);
field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
darkModeFlag = field.getInt(layoutParams);
Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class);
if (type == 0) {
extraFlagField.invoke(window, tranceFlag, tranceFlag);//只需要状态栏透明
} else if (type == 1) {
extraFlagField.invoke(window, tranceFlag | darkModeFlag, tranceFlag | darkModeFlag);//状态栏透明且黑色字体
} else {
extraFlagField.invoke(window, 0, darkModeFlag);//清除黑色字体
}
} catch (Exception e) {

}
}

//魅族:
public static boolean setStatusBarDarkIcon(Window window, boolean dark) {
boolean result = false;
if (window != null) {
try {
WindowManager.LayoutParams lp = window.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);
window.setAttributes(lp);
result = true;
} catch (Exception e) {
Log.e("MeiZu", "setStatusBarDarkIcon: failed");
}
}
return result;
}
private static boolean isMiUIV6() {
try {
final BuildProperties prop = BuildProperties.newInstance();
String name = prop.getProperty(KEY_MIUI_VERSION_NAME, "");
if ("V6".equals(name)) {
return true;
} else {
return false;
}
//            return prop.getProperty(KEY_MIUI_VERSION_CODE, null) != null
//                    || prop.getProperty(KEY_MIUI_VERSION_NAME, null) != null
//                    || prop.getProperty(KEY_MIUI_INTERNAL_STORAGE, null) != null;
} catch (final IOException e) {
return false;
}
}

public static boolean isMIUI() {
try {
final BuildProperties prop = BuildProperties.newInstance();
return prop.getProperty(KEY_MIUI_VERSION_CODE, null) != null
|| prop.getProperty(KEY_MIUI_VERSION_NAME, null) != null
|| prop.getProperty(KEY_MIUI_INTERNAL_STORAGE, null) != null;
} catch (final IOException e) {
return false;
}
}

public static boolean isFlyme() {
try {
// Invoke Build.hasSmartBar()
final Method method = Build.class.getMethod("hasSmartBar");
return method != null;
} catch (final Exception e) {
return false;
}

}
}


//引用到的工具类

import android.os.Environment;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;

public class BuildProperties {

private final Properties properties;

private BuildProperties() throws IOException {
properties = new Properties();
properties.load(new FileInputStream(new File(Environment.getRootDirectory(), "build.prop")));
}

public static BuildProperties newInstance() throws IOException {
return new BuildProperties();
}

public boolean containsKey(final Object key) {
return properties.containsKey(key);
}

public boolean containsValue(final Object value) {
return properties.containsValue(value);
}

public Set<Entry<Object, Object>> entrySet() {
return properties.entrySet();
}

public String getProperty(final String name) {
return properties.getProperty(name);
}

public String getProperty(final String name, final String defaultValue) {
return properties.getProperty(name, defaultValue);
}

public boolean isEmpty() {
return properties.isEmpty();
}

public Enumeration<Object> keys() {
return properties.keys();
}

public Set<Object> keySet() {
return properties.keySet();
}

public int size() {
return properties.size();
}

public Collection<Object> values() {
return properties.values();
}
}


但是开启沉浸状态栏后,回出现

android:windowSoftInputMode=”adjustResize|stateHidden”


失效

这时候需要在有EditText需要弹出输入法的页面的根目录里添加

android:fitsSystemWindows="true"


然后处理一下状态栏背景色
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: