private LinearLayout logo_layout;private ImageView iv_logo;private int sh;private int layoutH;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);logo_layout=(LinearLayout) findViewById(R.id.logo_layout);iv_logo=(ImageView) findViewById(R.id.iv_logo);//获取屏幕的高度DisplayMetrics dm = new DisplayMetrics();WindowManager windowMgr = (WindowManager)getSystemService(Context.WINDOW_SERVICE);windowMgr.getDefaultDisplay().getMetrics(dm);sh = dm.heightPixels;logo_layout.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {// 当layout执行结束后回调此方法@Overridepublic void onGlobalLayout() {logo_layout.getViewTreeObserver().removeGlobalOnLayoutListener(this);layoutH = logo_layout.getHeight();}});//当键盘弹起的时候用屏幕的高度减去布局的高度,同时获取到键盘的高度,用键盘的高度和剩余的高度做对比SoftKeyBoardListener.setListener(MainActivity.this, new OnSoftKeyBoardChangeListener() {@Overridepublic void keyBoardShow(int height) {//键盘弹起回调int h=sh-layoutH;if(h>height){//高度大于键盘的高度setLayoutH(80);}else{//高度小于键盘的高度int resetH=Math.abs(height+layoutH-sh);setLayoutH(resetH);}}@Overridepublic void keyBoardHide(int height) {//键盘隐藏回调setLayoutH(80);}}); }/** * 重新设置布局高度 */private void setLayoutH(int h){LinearLayout.LayoutParams layoutParams = (android.widget.LinearLayout.LayoutParams) iv_logo.getLayoutParams();layoutParams.topMargin=dip2px(MainActivity.this, h);iv_logo.setLayoutParams(layoutParams);}/*** 根据手机的分辨率从 dp 的单位 转成为 px(像素)*/ public static int dip2px(Context context,float dpValue) { final float scale =context.getResources().getDisplayMetrics().density; return (int) (dpValue * scale + 0.5f); }
private View rootView;//activity的根视图int rootViewVisibleHeight;//纪录根视图的显示高度private OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener;public SoftKeyBoardListener(Activity activity) {//获取activity的根视图rootView = activity.getWindow().getDecorView();//监听视图树中全局布局发生改变或者视图树中的某个视图的可视状态发生改变rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {@Overridepublic void onGlobalLayout() {//获取当前根视图在屏幕上显示的大小Rect r = new Rect();rootView.getWindowVisibleDisplayFrame(r);int visibleHeight = r.height();if (rootViewVisibleHeight == 0) {rootViewVisibleHeight = visibleHeight;return;}//根视图显示高度没有变化,可以看作软键盘显示/隐藏状态没有改变if (rootViewVisibleHeight == visibleHeight) {return;}//根视图显示高度变小超过200,可以看作软键盘显示了if (rootViewVisibleHeight - visibleHeight > 200) {if (onSoftKeyBoardChangeListener != null) {onSoftKeyBoardChangeListener.keyBoardShow(rootViewVisibleHeight - visibleHeight);}rootViewVisibleHeight = visibleHeight;return;}//根视图显示高度变大超过200,可以看作软键盘隐藏了if (visibleHeight - rootViewVisibleHeight > 200) {if (onSoftKeyBoardChangeListener != null) {onSoftKeyBoardChangeListener.keyBoardHide(visibleHeight - rootViewVisibleHeight);}rootViewVisibleHeight = visibleHeight;return;}}});}private void setOnSoftKeyBoardChangeListener(OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener) {this.onSoftKeyBoardChangeListener = onSoftKeyBoardChangeListener;}public interface OnSoftKeyBoardChangeListener {void keyBoardShow(int height);void keyBoardHide(int height);}public static void setListener(Activity activity, OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener) {SoftKeyBoardListener softKeyBoardListener = new SoftKeyBoardListener(activity);softKeyBoardListener.setOnSoftKeyBoardChangeListener(onSoftKeyBoardChangeListener);}以上做了仔细说明了,运行效果如下:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。