android输入法把底部顶起来,Android 输入法将底部布局顶上去遮挡布局问题
最近在項目中遇到使用ReletiveLayout將View固定在底部,但彈出輸入框時底部的View被頂上去的問題, 嘗試了很多解決辦法 ,如下:
方法一:在你的activity中的oncreate中setContentView之前寫上這個代碼getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
方法二:在項目的AndroidManifest.xml文件中界面對應的里加入android:windowSoftInputMode="stateVisible|adjustResize",這樣會讓屏幕整體上移。如果加上的是
android:windowSoftInputMode="adjustPan"這樣鍵盤就會覆蓋屏幕。
方法三:把頂級的layout替換成ScrollView,或者說在頂級的Layout上面再加一層ScrollView的封裝。這樣就會把軟鍵盤和輸入框一起滾動了,軟鍵盤會一直處于底部。
方法四:更換根布局
方法五:在根布局下加上fitsSystemwindows = true
嘗試后發現:
方法一無效(ReletiveLayout下設置屬性無效)
方法二無效(ReletiveLayout下設置屬性無效)
方法三有效,但不適合 (布局沒有超過屏幕,使用ScroollView使得View都挨在一起,只能通過magin值控制距離,不準確)
方法四無效
方法五無效
試過這么多都沒用,真沒辦法了,最后只能監聽軟鍵盤的顯示和隱藏來控制 底部布局的顯隱,這樣可以解決問題,不過有點閃動,問題不是很大。
照搬的代碼,監聽代碼如下 :
public class KeyboardStateObserver {
private static final String TAG = KeyboardStateObserver.class.getSimpleName();
public static KeyboardStateObserver getKeyboardStateObserver(Activity activity) {
return new KeyboardStateObserver(activity);
}
private View mChildOfContent;
private int usableHeightPrevious;
private OnKeyboardVisibilityListener listener;
public void setKeyboardVisibilityListener(OnKeyboardVisibilityListener listener) {
this.listener = listener;
}
private KeyboardStateObserver(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();
}
});
}
private void possiblyResizeChildOfContent() {
int usableHeightNow = computeUsableHeight();
if (usableHeightNow != usableHeightPrevious) {
int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight();
int heightDifference = usableHeightSansKeyboard - usableHeightNow;
if (heightDifference > (usableHeightSansKeyboard / 4)) {
if (listener != null) {
listener.onKeyboardShow();
}
} else {
if (listener != null) {
listener.onKeyboardHide();
}
}
usableHeightPrevious = usableHeightNow;
Log.d(TAG,"usableHeightNow: " + usableHeightNow + " | usableHeightSansKeyboard:" + usableHeightSansKeyboard + " | heightDifference:" + heightDifference);
}
}
private int computeUsableHeight() {
Rect r = new Rect();
mChildOfContent.getWindowVisibleDisplayFrame(r);
Log.d(TAG,"rec bottom>" + r.bottom + " | rec top>" + r.top);
return (r.bottom - r.top);// 全屏模式下: return r.bottom
}
public interface OnKeyboardVisibilityListener {
void onKeyboardShow();
void onKeyboardHide();
}
}
在界面中使用:
KeyboardStateObserver.getKeyboardStateObserver(this).
setKeyboardVisibilityListener(new KeyboardStateObserver.OnKeyboardVisibilityListener() {
@Override
public void onKeyboardShow() {
Toast.makeText(MainActivity.this,"鍵盤彈出",Toast.LENGTH_SHORT).show();
}
@Override
public void onKeyboardHide() {
Toast.makeText(MainActivity.this,"鍵盤收回",Toast.LENGTH_SHORT).show();
}
});
總結
以上是生活随笔為你收集整理的android输入法把底部顶起来,Android 输入法将底部布局顶上去遮挡布局问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 旧金山大学网站的红黑树演示动画,益于理解
- 下一篇: 小学计算机的一些课题,小学信息技术课题申