relativeLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener(){ @Override public void onGlobalLayout(){ Rect r = new Rect(); relativeLayout.getWindowVisibleDisplayFrame(r); int screenHeight = relativeLayout.getRootView().getHeight(); int heightDifference = screenHeight - (r.bottom - r.top); Log.e("Keyboard Size",""+ heightDifference); if(height==0){ height=heightDifference;//状态栏高度 } if(heightDifference>100){ LinearLayout.LayoutParams layoutParams= (LinearLayout.LayoutParams) relativeLayout.getLayoutParams(); layoutParams.bottomMargin=heightDifference-height; //软键盘高度 relativeLayout.setLayoutParams(layoutParams); } //boolean visible = heightDiff > screenHeight / 3; } });