提供沉浸式状态栏,可调用黑色状态栏模式,判断了小米MIUI和flyme系统独立的黑色状态栏样式可独立调用,提供了一些简单的吐司和生成日志的方法,可以直接调用。
Screenshot_20160427-151831.png
因为有项目需要开发一个浅色调背景的Activity且需要状态栏沉浸式样式,但目前Android4.4.4以上支持沉浸但在Android6.0以下不支持黑色内容的状态栏文字图标,所以写了一个单独的Activity类做实现:
import android.app.Activity;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.Toast;
import com.kongzue.find.R;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
/**
* Created by chao on 2016/3/3.
* 半透明状态栏形式的Activity
* 以及对原生安卓、MIUI、flyme的透明状态栏显示灰色图标文字的支持
* 我只想说太tm麻烦了
*/
public class BaseActivity extends Activity {
protected SystemBarTintManager mTintManager;
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";
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 {
final Method method = Build.class.getMethod("hasSmartBar");
return method != null;
} catch (final Exception e) {
return false;
}
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
setTranslucentStatus(true);
}
mTintManager = new SystemBarTintManager(this);
}
protected void setTranslucentStatus(boolean on) {
Log.i(">>>",Build.VERSION.SDK_INT+"");
if (isMIUI())setStatusBarDarkMode(true,this);
if (isFlyme())setStatusBarDarkIcon(getWindow(),true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Window window = 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);
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN|View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
getWindow().setStatusBarColor(Color.TRANSPARENT);
return;
}else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
Window win = getWindow();
WindowManager.LayoutParams winParams = win.getAttributes();
final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
if (on) {
winParams.flags |= bits;
} else {
winParams.flags &= ~bits;
}
win.setAttributes(winParams);
return;
}
}
public void setStatusBarDarkMode(boolean darkmode, Activity activity) {
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);
} catch (Exception e) {
e.printStackTrace();
}
}
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;
}
public static class BuildProperties {
private final Properties properties;
private BuildProperties() throws IOException {
properties = new Properties();
properties.load(new FileInputStream(new File(Environment.getRootDirectory(), "build.prop")));
}
public boolean containsKey(final Object key) {
return properties.containsKey(key);
}
public boolean containsValue(final Object value) {
return properties.containsValue(value);
}
public Set<Map.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();
}
public static BuildProperties newInstance() throws IOException {
return new BuildProperties();
}
}
/**
* 设置占位符布局
*/
public void setStatusBarHeight() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
LinearLayout linear_bar = (LinearLayout) findViewById(R.id.sys_statusBar);
linear_bar.setVisibility(View.VISIBLE);
int statusHeight = getStatusBarHeight();
android.widget.RelativeLayout.LayoutParams params = (android.widget.RelativeLayout.LayoutParams) linear_bar.getLayoutParams();
params.height = statusHeight;
linear_bar.setLayoutParams(params);
}
}//end setStatusBarHeight()
/**
* 获取状态栏的高度
* @return
*/
private int getStatusBarHeight(){
try
{
Class<?> c=Class.forName("com.android.internal.R$dimen");
Object obj=c.newInstance();
Field field=c.getField("status_bar_height");
int x=Integer.parseInt(field.get(obj).toString());
return getResources().getDimensionPixelSize(x);
}catch(Exception e){
e.printStackTrace();
}
return 0;
}
protected final static String NULL = "";
private Toast toast;
protected void runOnMain(Runnable runnable) {
runOnUiThread(runnable);
}
//简易吐司
public void toast(final Object obj) {
try {
runOnMain(new Runnable() {
@Override
public void run() {
if (toast == null)
toast = Toast.makeText(BaseActivity.this, NULL,Toast.LENGTH_SHORT);
toast.setText(obj.toString());
toast.show();
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
//简易Log
public void log(final Object obj){
try {
runOnMain(new Runnable() {
@Override
public void run() {
if (BuildConfig.DEBUG) {
Log.i("log", obj.toString());
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}
需注意,
使用方式是将自己工程中的Activity extends BaseActivity即可;
需要使用吐司和log产生提示和日志时非特殊需求可以使用BaseActivity自带的toast()方法和log()方法来执行;
BuildConfig以及R类需要根据工程单独引用;
其次,在Activity的xml中需要一个LinearLayout来进行状态栏占位,Activity中的内容可放在一个RelativeLayout并将RelativeLayout的android:layout_below设为"@+id/sys_statusBar"
的具体代码如下:
<LinearLayout
android:id="@+id/sys_statusBar"
android:layout_width="fill_parent"
android:layout_height="0.1dp"
android:orientation="vertical"
android:visibility="gone">
</LinearLayout>
在Activity的onCreate方法中执行setStatusBarHeight();即可完成工作。