
大概就這種效果
額,突然發(fā)現(xiàn)UC被我卸載了,這個(gè)是QQ瀏覽器的效果,不過都一樣,如果當(dāng)前頁(yè)面不是全屏的話,把根布局設(shè)為相對(duì)布局,然后設(shè)置評(píng)論布局為處于底部,這樣在點(diǎn)擊評(píng)論時(shí)彈開鍵盤會(huì)觸發(fā)布局重繪,底部的評(píng)論也會(huì)處于軟鍵盤的上方,鍵盤消息,布局下移,但是我們要說的是全屏的情況下我們?nèi)绾翁幚怼?br> 首先要監(jiān)聽到軟件盤彈起,然后再設(shè)置評(píng)論的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);
}
}
然后再對(duì)popupWindow進(jìn)行設(shè)置位置
private void showSendMsgPop() {
if (null == sendMsgPopup) {
sendMsgPopup = new SendMsgPopupWindow(NewsDetailsActivity.this);
}
//設(shè)置處于底部
sendMsgPopup.showAtLocation(getWindow().getDecorView(), Gravity.BOTTOM, 0, 0);
}
當(dāng)然,最后最關(guān)鍵的是popupWindow里的配置,要不然popupWindow會(huì)被軟鍵盤遮擋。
@Override
public void dismiss() {
super.dismiss();
// 評(píng)論框消息的同時(shí)把軟鍵盤關(guān)閉
KeyBoardUtils.closeKeybord(editText, mContext);
}
public void showAtLocation(View parent, int gravity, int x, int y) {
// 這三行配置是關(guān)鍵
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); // 在顯示輸入法之后調(diào)用,否則popupwindow會(huì)在窗口底層
super.showAtLocation(parent, gravity, x, y);
}