上次說有時間將要寫一寫軟鍵盤遮擋佈局,在我實際開發中所踩過的坑.這裏我將寫下對於軟鍵盤遮擋佈局的問題記錄我的心得.
有些情況在佈局文件中會使用EditText這個控件,所以在對EditText輸入內容的時候會調用軟鍵盤將其彈出,這時將有可能遮擋輸
入的位置,所以需要想辦法將佈局往上頂,讓軟鍵盤不遮擋控件.
1.當佈局文件中沒有WebView解決方案:
可以在AndroidMainfest中講對應的activity配置
android:windowSoftInputMode="adjustResize" 這個屬性就可
如果在實際的開發中要取消EditText的焦點可以在其父控件中添加
android:focusable="true" android:focusableInTouchMode="true"這兩個屬性即可,從而可以通過軟鍵盤的顯示與隱藏來對EditText焦點的控制
editText.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() {
//判斷軟鍵盤是否顯示 if (KeyBoardUtils.isKeyboardShown(holder.mLlRvItem.getRootView())) { holder.mEtCommitOrdermsg.setCursorVisible(true); } else { holder.mEtCommitOrdermsg.setCursorVisible(false); } } });判斷軟鍵盤顯示與隱藏的方法請查看的我上一遍文章中有提到.
2 如果配置該屬性不能解決問題的時候可以使用如下的代碼解決問題:
public class KeyBoardToUiUp { public static void assistActivity (Activity activity) { new KeyBoardToUiUp(activity); } private View mChildOfContent; private int usableHeightPrevious; private FrameLayout.LayoutParams frameLayoutParams; private KeyBoardToUiUp(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(); if (usableHeightNow != usableHeightPrevious) { int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight(); int heightDifference = usableHeightSansKeyboard - usableHeightNow; if (heightDifference > (usableHeightSansKeyboard/4)) { frameLayoutParams.height = usableHeightSansKeyboard - heightDifference; } else { frameLayoutParams.height = usableHeightSansKeyboard; } mChildOfContent.requestLayout(); usableHeightPrevious = usableHeightNow; } }
private int computeUsableHeight() { Rect r = new Rect(); mChildOfContent.getWindowVisibleDisplayFrame(r); return r.bottom; } }
只需要在佈局遮擋的activity的onCreat()方法中的setContentView()的後面添加KeyBoardToUiUp.assistActivity(this);
方法即可.
備註:computeUsableHeight() 方法返回值根據fitSystemWindows的設置值來決定,如果佈局中fitsSystemWindows="false",
return r.bottom; 如果fitsSystemWindows="true", return (r.bottom - r.top);
3.如果是上面的方法都解決不了可以使用最後一種方法
/** * @param rootView 跟佈局 * @param bottomView 最後要顯示的控件
注意使用該方法的時候,在佈局中需要將父佈局改爲ScrollView才能使用
*/
public static void addLayoutListener(final View rootView, final View bottomView) { rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { Rect rect = new Rect(); rootView.getWindowVisibleDisplayFrame(rect);//獲取rootView的可視區域 int invisibleHeight = rootView.getRootView().getHeight() - rect.bottom;//獲取rootView的不可視區域高度 if (invisibleHeight > 150 ) { //鍵盤顯示 int[] location = new int[2]; bottomView.getLocationInWindow(location); //獲取bottomView的座標 int scrollHeight = (location[1] + bottomView.getHeight()) - rect.bottom;//算出需要滾動的高度 if (scrollHeight != 0) {//防止界面元素改變調用監聽,使界面上下跳動,如驗證碼倒計時 rootView.scrollTo(0, scrollHeight); } } else { rootView.scrollTo(0, 0); } } }); }
下次將寫android7.0獲取手機照相機拍攝功能及將拍攝的照片加入相冊和獲取相冊中的圖片所遇到的坑,希望篇文章對你有所幫助謝謝!!!!!!!