先來說一下這個是干嘛的吧...
長時間寫各式各樣的Activity寫煩了,到處要打日志到處要吐司提示好麻煩,于是自己整理了個Activity基類來實現(xiàn)一些常用的功能。
之前那個《通用BaseActivity實現(xiàn)暗色狀態(tài)欄》http://www.itdecent.cn/p/fdf5c9fd21fc 是這個的1.0版本,這回的2.0做了很多增強。
用法很簡單,將開發(fā)的Activity exthends這個BaseActivity就可以用了。
以下是一點說明:
1.要實現(xiàn)通知欄透明(Android4.4.4+),請在你的xml布局中放置一個以下的布局塊,將它放到layout頂端,其他布局跟隨這個LinearLayout即可。
<LinearLayout
android:id="@+id/sys_statusBar"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:orientation="vertical"
android:visibility="gone">
</LinearLayout>
然后在初始化綁定完畢組件后,調(diào)用setStatusBarHeightByLayout(LinearLayout linearLayout)傳入上邊那個sys_statusBar就可以了。在Android4.4.4以下不支持透明通知欄的情況下這個布局會隱藏,支持的情況下這個布局會顯示并且高度為通知欄的高度,此時Activity實際占用界面面積是全屏的,所以sys_statusBar實際上充當了占位布局占用了狀態(tài)欄的位置,如果設置了layout主布局的背景,是可以透明到狀態(tài)欄的。
至于為什么不將sys_statusBar設置個固定高度的原因是,部分Android改的系統(tǒng),例如miui,狀態(tài)欄高度是不同于原生安卓的,所以需要動態(tài)獲取狀態(tài)欄高度,使用占位布局強行將界面中其他元素下移一定的位置,實現(xiàn)狀態(tài)欄透明的原理。
2.我默認透明后是灰色狀態(tài)欄圖標文字樣式(Android6.0可見,如果不需要,請刪除代碼中有關(guān)“dark”的部分)
3.要簡易吐司一些文字(短時間的提醒),直接使用toast(Object obj);即可,相對于原始的吐司,優(yōu)勢在于方便以及不沖突,也就是說可以連續(xù)吐司,不會滯后消息。
4.簡易log打印,直接使用log(String str);即可,還是那句話,方便。
5.打開軟鍵盤和關(guān)閉軟鍵盤直接用openIMM(EditText editText,boolean openIMM);即可,傳入一個edittext和boolean決定開關(guān)就可以了,很方便。
6.以后還會陸續(xù)添加新的功能。
上代碼:
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 setStatusBarHeightByLayout(LinearLayout linearLayout) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
LinearLayout linear_bar = linearLayout;
linear_bar.setVisibility(View.VISIBLE);
int statusHeight = getStatusBarHeight();
android.widget.LinearLayout.LayoutParams params = (android.widget.LinearLayout.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();
}
}
public void openIMM(EditText editText,boolean openIMM){
if (openIMM) {
editText.setFocusable(true);
editText.requestFocus();
editText.setFocusableInTouchMode(true);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED);
}else{
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
}
}
}