原因:未计算底部状态栏,导致排版上移高度不对。
处理方案:直接监听GlobalLayout,获得IME的高度,自己进行上移操作。
private void registCustomLayout() {
layoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
View focus = mActivity.getCurrentFocus();
//edit失去焦点,下移界面
if(!(focus instanceof Cocos2dxEditBox)){
if(isCustomLayout) mActivity.getResizeLayout().scrollTo(0,0);
Log.d("Cocos2dxEditBox", "hideLayout "+isCustomLayout);
isCustomLayout = false;//重置
return;
}
Rect r = new Rect();
getWindowVisibleDisplayFrame(r);
int screenHeight = getRootView().getHeight();
int imeHeight = screenHeight - (r.bottom - r.top);
if(isCustomLayout && imeHeight <=0){
mActivity.getGLSurfaceView().requestFocus();
Log.d("Cocos2dxEditBox", "requestFocus");
return;
}
//获得焦点,判断是否需要进行自定义排版
if(!isCustomLayout){
Cocos2dxEditBox editbox = (Cocos2dxEditBox)focus;
FrameLayout.LayoutParams editBoxParams = (FrameLayout.LayoutParams)editbox.getLayoutParams();
int bottomHeight = screenHeight - editBoxParams.topMargin - editBoxParams.height; //editbox底部到屏幕底部的高度
if(bottomHeight >=0 && imeHeight > bottomHeight){
isCustomLayout = true; //不需要再次排版
ResizeLayout layout = mActivity.getResizeLayout();
int upOffset = imeHeight - bottomHeight;//界面上移高度
layout.scrollTo(0,upOffset);
}
Log.d("Cocos2dxEditBox", "isCustomLayout "+isCustomLayout);
}
}
};
getViewTreeObserver().addOnGlobalLayoutListener(layoutListener);
}
在editbox移除时取消监听
public void onRemove(){
if(layoutListener != null){
getViewTreeObserver().removeOnGlobalLayoutListener(layoutListener);
}
}