packagecom.ph.demo.test.two;
importandroid.app.Activity;
importandroid.content.Context;
importandroid.content.res.Resources;
importandroid.graphics.Color;
importandroid.graphics.Rect;
importandroid.os.Build;
importandroid.os.Bundle;
importandroid.support.v7.app.AppCompatActivity;
importandroid.util.Log;
importandroid.view.View;
importandroid.view.ViewConfiguration;
importandroid.view.ViewTreeObserver;
importandroid.view.Window;
importandroid.view.WindowManager;
importandroid.widget.Button;
importandroid.widget.ListView;
importandroid.widget.RelativeLayout;
importandroid.widget.SimpleAdapter;
importandroid.widget.TextView;
importandroid.widget.Toast;
importcom.ph.demo.test.R;
importcom.ph.demo.test.ViewUtils;
importjava.lang.reflect.Field;
importjava.lang.reflect.Method;
importjava.util.ArrayList;
importjava.util.List;
import staticcom.ph.demo.test.ViewUtils.TAG;
public classMainActivityTwoextendsActivity {
ViewdecorView;
//Bar布局监听器
ViewTreeObserver.OnGlobalLayoutListeneronGlobalLayoutListener;
//记录上一次底部的间距
intbottomSign=0;
@Override
protected voidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.LOLLIPOP) {
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);
}else{
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
setContentView(R.layout.activity_main_two);
decorView= getWindow().getDecorView();
// final TextView textView = (TextView) findViewById(R.id.tv_hide);
finalTextView textView = (TextView) findViewById(R.id.tv_hide);
Button navHide = (Button) findViewById(R.id.btn_nav_hide);
navHide.setOnClickListener(newView.OnClickListener() {
@Override
public voidonClick(View view) {
toggleHideyBar();
}
});
ListView listView = (ListView) findViewById(R.id.lv_info);
List list =newArrayList<>();
for(inti =0;i <30;i++) {
list.add("String "+ i);
}
twoAdapter twoAdapter =newtwoAdapter();
twoAdapter.initAdapter(this,list);
listView.setAdapter(twoAdapter);
ViewUtils.setViewGroupHeightBasedOnChildren(listView);
finalRelativeLayout.LayoutParams olp = (RelativeLayout.LayoutParams) textView.getLayoutParams();
addOnSoftKeyBoardVisibleListener(newIKeyBoardVisibleListener() {
@Override
public voidonSoftKeyBoardVisible(intwindowBottom) {
olp.setMargins(0,0,0,windowBottom);
textView.setLayoutParams(olp);
}
});
}
interfaceIKeyBoardVisibleListener {
voidonSoftKeyBoardVisible(intwindowBottom);
}
public void addOnSoftKeyBoardVisibleListener(finalIKeyBoardVisibleListener listener) {
onGlobalLayoutListener=newViewTreeObserver.OnGlobalLayoutListener() {
@Override
public voidonGlobalLayout() {
Rect rect =newRect();
decorView.getWindowVisibleDisplayFrame(rect);
//计算出可见屏幕的高度
intdisplayHight = rect.bottom- rect.top;
//获得屏幕整体的高度
inthight =decorView.getHeight();
//获得键盘高度
intkeyboardHeight = hight - displayHight - getStatusBarHeight();
if(bottomSign!= keyboardHeight) {
bottomSign= keyboardHeight;
listener.onSoftKeyBoardVisible(keyboardHeight);
}
}
};
decorView.getViewTreeObserver().addOnGlobalLayoutListener(onGlobalLayoutListener);
}
private static intgetNavigationBarHeight(Context context) {
intnavigationBarHeight =0;
Resources rs = context.getResources();
intid = rs.getIdentifier("navigation_bar_height","dimen","android");
if(id >0&&hasNavBar(context)) {
navigationBarHeight = rs.getDimensionPixelSize(id);
}
returnnavigationBarHeight;
}
/**
*检查是否存在虚拟按键栏
*
*@paramcontext
*@return
*/
public static booleanhasNavBar(Context context) {
Resources res = context.getResources();
//这种方式一定要注意写法要正确,内部应该是通过反射去调用的。
intresourceId = res.getIdentifier("config_showNavigationBar","bool","android");
if(resourceId !=0) {
booleanhasNav = res.getBoolean(resourceId);
// check override flag
String sNavBarOverride =getNavBarOverride();
if("1".equals(sNavBarOverride)) {
hasNav =false;
}else if("0".equals(sNavBarOverride)) {
hasNav =true;
}
returnhasNav;
}else{// fallback
return!ViewConfiguration.get(context).hasPermanentMenuKey();
}
}
/**
*判断虚拟按键栏是否重写
*
*@return
*/
private staticStringgetNavBarOverride() {
String sNavBarOverride =null;
if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.KITKAT) {
try{
Class c = Class.forName("android.os.SystemProperties");
Method m = c.getDeclaredMethod("get",String.class);
m.setAccessible(true);
sNavBarOverride = (String) m.invoke(null,"qemu.hw.mainkeys");
}catch(Throwable e) {
}
}
returnsNavBarOverride;
}
/**
*获取顶部状态栏的高度
*
*@return
*/
private intgetStatusBarHeight() {
Class c =null;
Object obj =null;
Field field =null;
intx =0,sbar =0;
try{
c = Class.forName("com.android.internal.R$dimen");
obj = c.newInstance();
field = c.getField("status_bar_height");
x = Integer.parseInt(field.get(obj).toString());
sbar =this.getResources().getDimensionPixelSize(x);
}catch(Exception e1) {
e1.printStackTrace();
}
returnsbar;
}
/**
*隐藏和显示顶部底部状态栏
*
*@return
*/
public voidtoggleHideyBar() {
intuiOptions = getWindow().getDecorView().getSystemUiVisibility();
intnewUiOptions = uiOptions;
booleanisImmersiveModeEnabled =
((uiOptions | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY) == uiOptions);
if(isImmersiveModeEnabled) {
Log.i(TAG,"Turning immersive mode mode off. ");
}else{
Log.i(TAG,"Turning immersive mode mode on.");
}
// Navigation bar hiding: Backwards compatible to ICS.
if(Build.VERSION.SDK_INT>=14) {
newUiOptions ^= View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
}
// Status bar hiding: Backwards compatible to Jellybean
// if (Build.VERSION.SDK_INT >= 16) {
// newUiOptions ^= View.SYSTEM_UI_FLAG_FULLSCREEN;
// }
// Immersive mode: Backward compatible to KitKat.
// Note that this flag doesn't do anything by itself, it only augments the behavior
// of HIDE_NAVIGATION and FLAG_FULLSCREEN. For the purposes of this sample
// all three flags are being toggled together.
// Note that there are two immersive mode UI flags, one of which is referred to as "sticky".
// Sticky immersive mode differs in that it makes the navigation and status bars
// semi-transparent, and the UI flag does not get cleared when the user interacts with
// the screen.
if(Build.VERSION.SDK_INT>=18) {
newUiOptions ^= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
}
getWindow().getDecorView().setSystemUiVisibility(newUiOptions);
}
@Override
protected voidonDestroy() {
super.onDestroy();
decorView.getViewTreeObserver().removeOnGlobalLayoutListener(onGlobalLayoutListener);
}
}