彈窗或Dialog下,彈出打開輸入法時败明,頁面被遮擋隘马。需求是輸入法彈出時頁面需要上移,且輸入框不被擋住妻顶,試了改變布局的高度酸员,結(jié)果并不理想
/**
* @param root? ? ? ? ? ? 最外層布局
* @param needToScrollView 要滾動的布局,就是說在鍵盤彈出的時候,你需要試圖滾動上去的View,在鍵盤隱藏的時候,他又會滾動到原來的位置的布局
*/
private void controlKeyboardLayout(Context context,final View root, final View needToScrollView) {
root.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
private Rectr =new Rect();
? ? ? ? @Override
? ? ? ? public void onGlobalLayout() {
Rect rect =new Rect();
? ? ? ? ? ? //獲取root在窗體的可視區(qū)域
? ? ? ? ? ? root.getWindowVisibleDisplayFrame(rect);
? ? ? ? ? ? //獲取root在窗體的不可視區(qū)域高度(被其他View遮擋的區(qū)域高度)
? ? ? ? ? ? int rootInvisibleHeight =root.getHeight() - rect.bottom;
? ? ? ? ? ? //若不可視區(qū)域高度大于100,則鍵盤顯示
? ? ? ? ? ? LogUtil.e("不可視區(qū)域:" + rootInvisibleHeight);
? ? ? ? ? ? if (rootInvisibleHeight > -1100) {
if (syDialog !=null){
syDialog.setStyle(1, R.style.loading_dialog);
? ? ? ? ? ? ? ? }
int[] location =new int[2];
? ? ? ? ? ? ? ? //獲取scrollToView在窗體的坐標(biāo)
? ? ? ? ? ? ? ? needToScrollView.getLocationInWindow(location);
? ? ? ? ? ? ? ? //計算root滾動高度讳嘱,使scrollToView在可見區(qū)域
? ? ? ? ? ? ? ? int scrollHeight1 = (location[1] +needToScrollView.getHeight()) - rect.bottom;
? ? ? ? ? ? ? ? int scrollHeight = (int) ScreenUtils.px2dp(context,550);
? ? ? ? ? ? ? ? LogUtil.e("scroll區(qū)域:" + scrollHeight1);
? ? ? ? ? ? ? ? if (scrollHeight ==0){
return;
? ? ? ? ? ? ? ? }
root.scrollTo(0, scrollHeight);
? ? ? ? ? ? }else {
//鍵盤隱藏
? ? ? ? ? ? ? ? root.scrollTo(0, 0);
? ? ? ? ? ? }
}
});
}
還試了manifest加android:windowSoftInputMode="adjustPan"幔嗦,也沒實現(xiàn)dialog上移的效果;
最后一行代碼就搞定了沥潭,就是改變Dialog的主題邀泉,syDialog.setStyle(DialogFragment.STYLE_NO_TITLE, R.style.loading_dialog);
主題代碼:
<style name="loading_dialog" parent="@android:style/Theme.Dialog">
? ? <item name="android:windowSoftInputMode">adjustPan
</style>
記錄一下!