首先这是一个全体布局往上的需求
rootLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Log.v("test", "布局变化");
Rect rect = new Rect();
rootLayout.getWindowVisibleDisplayFrame(rect);
Log.v("test", rect.bottom+" "+rect.top+" "+rect.left+" "+rect.right);
int screenHeight = getWindow().getDecorView().getHeight();
int naviBarHeight= SoftKeyboardFixerForFullscreen.getNavigationBarHeight(ReplyActivity.this);
int softHeight = screenHeight - rect.bottom-naviBarHeight;
Log.v("test", softHeight+" "+screenHeight);
Log.v("test","导航栏高度"+naviBarHeight);
if (softHeight > 0) {
rootLayout.scrollTo(0,softHeight);
} else {
rootLayout.scrollTo(0,0);
}
}
});
/**
* 获取Bar高度
*
* @param context context
* @param barName 名称
* @return Bar高度
*/
private static int getBarHeight(Context context, String barName) {
// 获得状态栏高度
int resourceId = context.getResources().getIdentifier(barName, "dimen", "android");
return context.getResources().getDimensionPixelSize(resourceId);
}
/**
* 是否有NavigationBar
*
* @param activity 上下文
* @return 是否有NavigationBar
*/
private static boolean hasNavBar(Activity activity) {
WindowManager windowManager = activity.getWindowManager();
Display d = windowManager.getDefaultDisplay();
DisplayMetrics realDisplayMetrics = new DisplayMetrics();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
d.getRealMetrics(realDisplayMetrics);
}
int realHeight = realDisplayMetrics.heightPixels;
int realWidth = realDisplayMetrics.widthPixels;
DisplayMetrics displayMetrics = new DisplayMetrics();
d.getMetrics(displayMetrics);
int displayHeight = displayMetrics.heightPixels;
int displayWidth = displayMetrics.widthPixels;
return (realWidth - displayWidth) > 0 || (realHeight - displayHeight) > 0;
}
/**
* 获取导航栏高度
*
* @param activity activity
* @return 导航栏高度
*/
public static int getNavigationBarHeight(Activity activity) {
if (hasNavBar(activity)) {
// 获得导航栏高度
return getBarHeight(activity, NAV_BAR_HEIGHT_RES_NAME);
} else {
return 0;
}
}
下面两个方法来源
https://blog.csdn.net/qq_35605213/article/details/81005187
网友评论