Android 仿UC瀏覽器詳情頁評論彈框效果

大概就這種效果
額,突然發現UC被我解除安裝了,這個是QQ瀏覽器的效果,不過都一樣,如果當前頁面不是全屏的話,把根佈局設為相對佈局,然後設定評論佈局為處於底部,這樣在點選評論時彈開鍵盤會觸發佈局重繪,底部的評論也會處於軟鍵盤的上方,鍵盤訊息,佈局下移,但是我們要說的是全屏的情況下我們如何處理。
首先要監聽到軟體盤彈起,然後再設定評論的popupWindow
import android.app.Activity; import android.graphics.Rect; import android.support.v4.app.Fragment; import android.view.View; import android.view.ViewTreeObserver; public class KeyboardStatusDetector { private static final int SOFT_KEY_BOARD_MIN_HEIGHT = 100; private KeyboardVisibilityListener mVisibilityListener; boolean keyboardVisible = false; public KeyboardStatusDetector registerFragment(Fragment f) { return registerView(f.getView()); } public KeyboardStatusDetector registerActivity(Activity a) { return registerView(a.getWindow().getDecorView().findViewById(android.R.id.content)); } public KeyboardStatusDetector registerView(final View v) { v.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { Rect r = new Rect(); v.getWindowVisibleDisplayFrame(r); //int heightDiff = v.getRootView().getHeight() - (r.bottom - r.top); int heightDiff = v.getRootView().getHeight() - r.bottom; LogUtils.e("鍵盤A--->", v.getRootView().getHeight() +""+ r.bottom +""+ r.top); if (heightDiff > SOFT_KEY_BOARD_MIN_HEIGHT) { // if more than 100 pixels, its probably a keyboard... if (!keyboardVisible) { keyboardVisible = true; if (mVisibilityListener != null) { mVisibilityListener.onVisibilityChanged(true, heightDiff); } } } else { if (keyboardVisible) { keyboardVisible = false; if (mVisibilityListener != null) { mVisibilityListener.onVisibilityChanged(false, heightDiff); } } } } }); return this; } public KeyboardStatusDetector setVisibilityListener(KeyboardVisibilityListener listener) { mVisibilityListener = listener; return this; } public interface KeyboardVisibilityListener { void onVisibilityChanged(boolean keyboardVisible, int heightDiff); } }
然後再對popupWindow進行設定位置
private void showSendMsgPop() { if (null == sendMsgPopup) { sendMsgPopup = new SendMsgPopupWindow(NewsDetailsActivity.this); } //設定處於底部 sendMsgPopup.showAtLocation(getWindow().getDecorView(), Gravity.BOTTOM, 0, 0); }
當然,最後最關鍵的是popupWindow裡的配置,要不然popupWindow會被軟鍵盤遮擋。
@Override public void dismiss() { super.dismiss(); // 評論框訊息的同時把軟鍵盤關閉 KeyBoardUtils.closeKeybord(editText, mContext); } public void showAtLocation(View parent, int gravity, int x, int y) { // 這三行配置是關鍵 this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); this.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED); // 在顯示輸入法之後呼叫,否則popupwindow會在視窗底層 super.showAtLocation(parent, gravity, x, y); }