Android webView 軟鍵盤覆蓋輸入框,webView頁面底部顯示不全
阿新 • • 發佈:2019-01-07
1.軟鍵盤覆蓋輸入框
【解決】
1.1AndroidManifest.xml
<activity
android:name=".activity.FindDetailsActivity"
android:windowSoftInputMode="stateHidden|adjustPan" />
1.2 新增類(該類是獨立類)
1.3 呼叫上面類-載入佈局頁面-載入佈局方法/** * 時間: 2016/7/21 11:50 * ---ZWQ--- */ public class AndroidBug5497Workaround { private static final String TAG = "AndroidBug5497Workaround"; // For more information, see https://code.google.com/p/android/issues/detail?id=5497 // To use this class, simply invoke assistActivity() on an Activity that already has its content view set. public static void assistActivity (Activity activity) { new AndroidBug5497Workaround(activity); } private Activity activity; private FrameLayout.LayoutParams frameLayoutParams; private View mChildOfContent; private int usableHeightPrevious; private int statusBarHeight;//狀態列高度 private AndroidBug5497Workaround(Activity activity) { this.activity = activity; FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content); mChildOfContent = content.getChildAt(0); mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { public void onGlobalLayout() {//全域性檢視 possiblyResizeChildOfContent(); } }); frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent.getLayoutParams(); } /**可能調整內容的子內容*/ private void possiblyResizeChildOfContent() { //獲得有效高度 int usableHeightNow = computeUsableHeight(); LogUtils.v(TAG,"有效高度:"+usableHeightNow); if (usableHeightNow != usableHeightPrevious) { int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight(); LogUtils.v(TAG,"全屏高度:"+usableHeightSansKeyboard); //這個判斷是為了解決19之前的版本不支援沉浸式狀態列導致佈局顯示不完全的問題(使用沉浸式狀態列時使用改判斷) // if(Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT){ // Rect frame = new Rect(); // activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame); // int statusBarHeight = frame.top; // usableHeightSansKeyboard -= statusBarHeight; // } int heightDifference = usableHeightSansKeyboard - usableHeightNow; LogUtils.v(TAG, "預設高度:" + heightDifference); if (heightDifference > (usableHeightSansKeyboard/4)) { // keyboard probably just became visible(鍵盤可能是可見的) frameLayoutParams.height = usableHeightNow; } else { // keyboard probably just became hidden(鍵盤可能是隱藏的) frameLayoutParams.height = usableHeightSansKeyboard-statusBarHeight; } mChildOfContent.requestLayout();//重新設定自己位置 usableHeightPrevious = usableHeightNow; } } /**計算有效高度*/ private int computeUsableHeight() { Rect r = new Rect(); mChildOfContent.getWindowVisibleDisplayFrame(r); statusBarHeight = r.top; LogUtils.v(TAG,"狀態列高度:"+statusBarHeight); LogUtils.v(TAG,"我的佈局高度(包含狀態列):"+r.bottom);//從頂到載入佈局控制元件底部距離 /* 這個判斷是為了解決19之後的版本在彈出軟鍵盤時, 鍵盤和推上去的佈局(adjustResize)之間有黑色區域的問題 使用沉浸式狀態列時使用改判斷(未檢驗用時調節)*/ // if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){ // return (r.bottom - r.top)+statusBarHeight; // } return (r.bottom - r.top); } }
@Override
public void setRootView() {
super.setRootView();
setContentView(R.layout.activity_find_details);
AndroidBug5497Workaround.assistActivity(this);//防止軟體盤遮擋網頁輸入框內容
context = this;
}
1.4完成
【說明】
通過改變AndroidBug5497Workaround 類解決 侵入式狀態列 中顯示網頁不全問題