本文主要介绍沉浸式的一种解决方案。
使用方法
实现onCreate() 返回你的主视图即可
public class MainActivity extends BaseActivityRex {
@Override
protected int onCreate() {
return R.layout.activity_main;
}
@Override
protected void initView() {
}
}
核心原理
1.根据资源ID获取状态栏高度 和手机带虚拟键盘高度 响应的尺寸值
根据版本打开沉浸式后,得到其占位可自定义其显示
或者设为0让它全屏,一般下方会有一些按钮 全屏按钮和 虚拟键盘重叠十分不好看。
2.动态布局添加 让主视图成为 一部分view。方便自定义一些共用布局,和视图操作。如过度画面啊 加载界面
或者 全局性的title bar都可以在添加statusView后 自行添加。
3.简化了下find id
都是些小东西 自己用起来觉得方便。分享给大家。由于经常要交接代码给别人,所以习惯用原生可见易理解的东西封装。
源码
package your package
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import java.lang.reflect.Method;
/**
* 简化activity流程
* 完美适配所有的可用沉浸式
* 如果该版本存在,直接操作statusView navigationView 可自定义沉浸式 具体显示的内容 颜色 并非要和 相连颜色一致
* 如果不想要占位,设为高度0即可 那么你的主视图内容就会延伸占到statusView navigationView位置
*/
public abstract class BaseActivityRex extends FragmentActivity {
private boolean is_new_version = false;
public Bundle savedInstanceState = null;
private Activity activity;
private View statusView;
private View navigationView;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activity = BaseActivityRex.this;
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
//竖屏
this.savedInstanceState = savedInstanceState;
if ((Intent.FLAG_ACTIVITY_CLEAR_TOP & getIntent().getFlags()) != 0) {
finish();
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//透明状态栏
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//透明导航栏
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
is_new_version = true;
} else {
is_new_version = false;
}
int mainUi = onCreate();
intSetContentView(mainUi);
onCreateWithBundle(savedInstanceState);
initView();
initData();
}
private void intSetContentView(int mainUiId) {
LinearLayout.LayoutParams lp_root = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0);
LinearLayout linearRoot = new LinearLayout(activity);
linearRoot.setOrientation(LinearLayout.VERTICAL);
setContentView(linearRoot);
//添加状态栏占位
LinearLayout.LayoutParams lp_status = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0);
statusView = new View(linearRoot.getContext());
linearRoot.addView(statusView, lp_status);
//添加主视图父容器
LinearLayout.LayoutParams lp_main_ui_parent = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0, 1);
LinearLayout main_ui_parent = new LinearLayout(linearRoot.getContext());
linearRoot.addView(main_ui_parent, lp_main_ui_parent);
//虚拟按键占位
navigationView = new View(linearRoot.getContext());
linearRoot.addView(navigationView, lp_status);
//添加主视图
if (mainUiId > 0) {
View view = View.inflate(activity, mainUiId, null);
main_ui_parent.addView(view);
}
}
protected void onCreateWithBundle(Bundle savedInstanceState) {
}
@Override
protected void onStart() {
super.onStart();
Resources resources = getResources();
if (!is_new_version) {
if (statusView != null) {
statusView.setVisibility(View.GONE);
}
} else {
/**
* 获取状态栏高度——方法1
* */
int statusBarHeight = -1;
//获取status_bar_height资源的ID
int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
statusView.setVisibility(View.VISIBLE);
//根据资源ID获取响应的尺寸值
statusBarHeight = getResources().getDimensionPixelSize(resourceId);
ViewGroup.LayoutParams lp = statusView.getLayoutParams();
lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
lp.height = statusBarHeight;
statusView.setLayoutParams(lp);
Log.d("rex", "statusBarHeight===" + statusBarHeight);
}
}
if (navigationView != null) {
if (getHasNavigationBar()) {
navigationView.setVisibility(View.VISIBLE);
int resourceId = resources.getIdentifier("navigation_bar_height",
"dimen", "android");
//动态获取NavigationBar的高度
int height = resources.getDimensionPixelSize(resourceId);
ViewGroup.LayoutParams lp = navigationView.getLayoutParams();
lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
lp.height = height;
navigationView.setLayoutParams(lp);
Log.d("rex", "navigation_bar_height===" + height);
// int resourceId2 = resources.getIdentifier("status_bar_height",
// "dimen", "android");
// int height2 = resources.getDimensionPixelSize(resourceId2);
// Log.d("rex", "height2===" + height2);
} else {
navigationView.setVisibility(View.GONE);
}
}
}
protected abstract int onCreate();//返回主布局
protected abstract void initView();
public void initData() {
}
public <T> T Fid(int id) {
T t = (T) findViewById(id);
return t;
}
/**
* 判断是否存在虚拟键盘
*
* @return
*/
public boolean getHasNavigationBar() {
boolean hasNavigationBar = false;
Resources rs = getResources();
int id = rs.getIdentifier("config_showNavigationBar", "bool", "android");
if (id > 0) {
hasNavigationBar = rs.getBoolean(id);
}
try {
Class systemPropertiesClass = Class.forName("android.os.SystemProperties");
Method m = systemPropertiesClass.getMethod("get", String.class);
String navBarOverride = (String) m.invoke(systemPropertiesClass, "qemu.hw.mainkeys");
if ("1".equals(navBarOverride)) {
hasNavigationBar = false;
} else if ("0".equals(navBarOverride)) {
hasNavigationBar = true;
}
} catch (Exception e) {
}
return hasNavigationBar;
}
}
/**
* 全透明状态栏和底部导航栏
*/
private fun showTransparentBar() {
// transparentStatusBar
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
or window.decorView.systemUiVisibility)
window.statusBarColor = Color.TRANSPARENT
window.isNavigationBarContrastEnforced = false
window.navigationBarColor = Color.TRANSPARENT
// transparentNavBar
val decorView: View = window.decorView
val option =
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
decorView.systemUiVisibility = decorView.systemUiVisibility or option
}