提供沉浸式狀態(tài)欄,可調用黑色狀態(tài)欄模式,判斷了小米MIUI和flyme系統(tǒng)獨立的黑色狀態(tài)欄樣式可獨立調用雨膨,提供了一些簡單的吐司和生成日志的方法松蒜,可以直接調用扔茅。
Screenshot_20160427-151831.png
因為有項目需要開發(fā)一個淺色調背景的Activity且需要狀態(tài)欄沉浸式樣式,但目前Android4.4.4以上支持沉浸但在Android6.0以下不支持黑色內容的狀態(tài)欄文字圖標秸苗,所以寫了一個單獨的Activity類做實現(xiàn):
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.
* 半透明狀態(tài)欄形式的Activity
* 以及對原生安卓召娜、MIUI、flyme的透明狀態(tài)欄顯示灰色圖標文字的支持
* 我只想說太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()
/**
* 獲取狀態(tài)欄的高度
* @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產(chǎn)生提示和日志時非特殊需求可以使用BaseActivity自帶的toast()方法和log()方法來執(zhí)行;
BuildConfig以及R類需要根據(jù)工程單獨引用檀咙;
其次雅倒,在Activity的xml中需要一個LinearLayout來進行狀態(tài)欄占位,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方法中執(zhí)行setStatusBarHeight();即可完成工作弧可。