最後一步就是看possiblyResizeChildOfContent()在做什麼事了
在開發App時 這個奇異的類倒底做了什麼呢? 這個minHeight 就能夠確保最少有 42dp 的高度 或是 但這個方式有個限制,就是不克不及有WebView 這問題分入門款 & 進階款 這裡就是做了一些運算得知目前的可顯示 UI 高度 再來設定 OnGlobalLayoutListener 來監聽 View 的可視狀態變化 以上藥方就能夠決意issue 5497的問題囉!!! 哈哈哈
讓華頓翻譯社們來看看吧
起首是剛剛我們呼叫的 AndroidBug5497Workaround.assistActivity(this)<EditText
android:id="@+id/groupBuy_contentEdit"
android:minHeight="42dp" />
這樣輸入框就不會被擋住了
Android 彈出軟鍵盤後也還會有42dp的高度存在<activity
android:name=".app.MainActivity"
android:windowSoftInputMode="adjustResize"/>
因為WebView是由網頁元件去call軟鍵盤出來的
在全屏模式的狀況下,兩個屬性都不能用
但若是不是全屏,adjustResize照樣可以用的
如下表
首先來講說入門款的藥方private void possiblyResizeChildOfContent() {
int usableHeightNow = computeUsableHeight();
if (usableHeightNow != usableHeightPrevious) {
int usableHeightSansKeyboard = mChildOfContent
.getRootView()
.getHeight();
int heightDifference =
usableHeightSansKeyboard - usableHeightNow;
if (heightDifference > (usableHeightSansKeyboard/4)) {
// keyboard probably just became visible
frameLayoutParams.height =
usableHeightSansKeyboard - heightDifference;
} else {
// keyboard probably just became hidden
frameLayoutParams.height = usableHeightSansKeyboard;
}
mChildOfContent.requestLayout();
usableHeightPrevious = usableHeightNow;
}
}
再進行重設
比較值得一提的是
usableHeightSansKeyboard/4
會有這個前提是因為
OnGlobalLayoutListener 監聽的事宜太多了
這裡多一個條件就是當高度的改變大於1/4的時辰
我們再進行重設的動作就好
避免沒必要要的重設
只要有變化,就履行 possiblyresizeChildOfContent()
在看 possiblyresizeChildOfContent() 之前
華頓翻譯社們先看下面這段 method
資料來曆:
https://www.diycode.cc/topics/383
http://stackoverflow.com/questions/7417123/android-how-to-adjust-layout-in-full-screen-mode-when-softkeyboard-is-visible/19494006#19494006
請這位利用者消消氣
我們今天就是來解決這個問題的
(偏方)
我在實做的時辰發現了一個小技能
如果只是要讓 EditText 可以看到
不要被擋住的話,只要設一個屬性
(進階款藥方)
此藥方是一個在stackoverflow裡
此中一位大神寫的類
使用方也很簡啦
只要在Activity 的 onCreate 中加上一句
AndroidBug5497Workaround.assistActivity(this) 即可
private View mChildOfContent;
private int usableHeightPrevious;
private FrameLayout.LayoutParams frameLayoutParams;
private AndroidBug5497Workaround(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();
}
前面的3個屬性是這個類的全域變數
從這段可以看到
我們找到 Activity 的 root View
並用 getCJildAt(0)
找到我們 Activity setContenView(View view) & setContent(int layRes) 的 View
<activity
android:name=".app.MainActivity"
android:windowSoftInputMode="adjustPan"/>
這兩個屬性都可以做到不擋畫面的結果
那麼差異在哪呢?
adjustResize 會重設畫面的高度
adjustPan 則是把整個畫面向上平移
(入門款藥方)
其實只要在Activity 插手一些屬性就好了
public static void assistActivity (Activity activity) {
new AndroidBug5497Workaround(activity);
}
這裡只是純真的一個接口
接著往 AndroidBug5497Workaround(activity) 看
private int computeUsableHeight() {
Rect r = new Rect();
mChildOfContent.getWindowVisibleDisplayFrame(r);
return (r.bottom - r.top);
}
這是為了獲得顯視中UI 高度所寫的method
其中的 r.bottom & r.top
看下圖應當會對照好理解
手機彈出的軟鍵盤卻大大的困擾呢?
利用者os:「鬼才知道華頓翻譯社輸入了什麼啊啊啊啊啊!!!!」
引用自: http://willy2016.pixnet.net/blog/post/123935690-willy%27s-fish%E6%95%99%E5%AD%B8%E7%AD%86%E8%A8%98%E有關翻譯的問題歡迎諮詢華頓翻譯社