以前寫註冊頁面的時候,UI同窗給我提了個意見,讓彈出軟鍵盤時候,左上角的標題「註冊」不動,中間內容往上移動,效果這樣
通過查閱資料和多方實踐,解決方法以下android
一、先要設置頁面軟鍵盤模式,這樣每次軟鍵盤彈出後佈局高度會減小軟鍵盤的高度ide
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
二、找出內容view,在每次佈局爲軟鍵盤彈出減少高度時監聽,改變內容view佈局
mVContent = findViewById(android.R.id.content); ViewTreeObserver.OnGlobalLayoutListener mLsnrLayout = new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout () { Rect rect = new Rect(); mVContent.getWindowVisibleDisplayFrame(rect); int height = rect.height(); boolean isImmShow = height < UcApp.sHeightPx; if ( isImmShow != mIsImmShow ) { mIsImmShow = isImmShow; mUbtnCommit.setVisibility(isImmShow ? View.INVISIBLE : View.VISIBLE); } } }; mVContent.getViewTreeObserver().addOnGlobalLayoutListener(mLsnrLayout);
XML格式是這樣的,標題我是在基類上設置加上的code
<!--註冊佈局--> <android.support.constraint.ConstraintLayout android:id="@+id/cl_register" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:visibility="visible">
public void setTitle (String txt) { if ( null == mTvTitle ) { mTvTitle = new TextView(UcApp.sCtx); ((FrameLayout) getWindow().getDecorView()).addView(mTvTitle); ViewUtils.inst(mTvTitle) .setMargins(119, 54, 0, 0) .setTextSize(42); mTvTitle.setTypeface(Typeface.DEFAULT_BOLD); mTvTitle.setTextColor(ComUtils.getClr(R.color.white_f2f2f2_60)); } mTvTitle.setText(txt); }
就是設置了marginserver
由於內容佈局是豎直居中的,標題是margin寫死的,因此佈局高度變了之後內容移動標題不動。get
但願這篇文章能幫助你們,大佬勿噴~it