我正在開發一個應用程序,其中包含活動和Fragment
。在片段佈局中,我使用相對佈局作爲父佈局,一個按鈕位於底部和滾動視圖之間。 Scrollview
包含editText框。如果我在scrollview中點擊最後的editTextBox
我的鍵盤隱藏了fragment
。我在清單中嘗試了adjustpan|adjustresize
,並且在我的fragment
中嘗試過,但尚未解決問題。安卓鍵盤調整大小
Q
安卓鍵盤調整大小
4
A
回答
0
這裏有Android的錯誤。在掙扎了很多之後,我能夠順利解決這個問題。這是一個單線解決方案,但它有一些預先要求。一條線是:
AndroidBug5497Workaround.assistActivity(this, R.id.LayoutInScrollView);
你的XML佈局必須是這樣的:
RelativeLayout{
HeaderView{}
ScrollView{
LinearLayout{
@+id/LayoutInScrollView
}
}
FooterView{} // the buttons u want to appear above keyboard
}
如果你不使用全屏幕,下面的類應該足夠:
class AndroidBug5497Workaround{
View svChildLayout;
int originalGravity;
Activity activity;
/**
* @param activity
* @param svChildLayoutId id of the layout that is the first child of the center ScrollView
*/
public static void assistActivity (Activity activity, int svChildLayoutId) {
new AndroidBug5497Workaround(activity, svChildLayoutId);
}
private AndroidBug5497Workaround(Activity activity, int svChildLayoutId) {
this.activity = activity;
svChildLayout = activity.findViewById(svChildLayoutId);
originalGravity = ((ScrollView.LayoutParams)svChildLayout.getLayoutParams()).gravity;
//Add listener
FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content);
mChildOfContent = content.getChildAt(0);
mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
public void onGlobalLayout() {
possiblyResizeChildOfContent2();
}
});
}
private void possiblyResizeChildOfContent2() {
int usableHeightNow = computeUsableHeight();
if (usableHeightNow != usableHeightPrevious) {
int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight();
int heightDifference = usableHeightSansKeyboard - usableHeightNow;
if (heightDifference > (usableHeightSansKeyboard/4)) {
// keyboard probably just became visible
onKeyboardVisible();
} else {
// keyboard probably just became hidden
onKeyboardHidden();
}
usableHeightPrevious = usableHeightNow;
}
}
private void onKeyboardVisible() {
ScrollView.LayoutParams params = (ScrollView.LayoutParams) svChildLayout.getLayoutParams();
params.gravity = Gravity.TOP;
svChildLayout.requestLayout();
final ScrollView parentSv = (ScrollView) svChildLayout.getParent();
parentSv.post(new Runnable() {
@Override
public void run() {
View focusedEditText = activity.getWindow().getCurrentFocus();
parentSv.smoothScrollTo(0, focusedEditText.getTop());
}
});
}
private void onKeyboardHidden() {
ScrollView.LayoutParams params = (ScrollView.LayoutParams) svChildLayout.getLayoutParams();
params.gravity = originalGravity;
svChildLayout.requestLayout();
}
}
如果您使用全屏幕,您需要以下類別(從windowSoftInputMode="adjustResize" not working with translucent action/navbar修改):
public class AndroidBug5497Workaround {
// For more information, see https://code.google.com/p/android/issues/detail?id=5497
// To use this class, simply invoke assistActivity() on an Activity that already has its content view set.
public static void assistActivity (Activity activity, int svChildLayoutId) {
new AndroidBug5497Workaround(activity, svChildLayoutId);
}
private View mChildOfContent;
private int usableHeightPrevious;
private FrameLayout.LayoutParams frameLayoutParams;
View svChildLayout;
int originalGravity;
Activity activity;
private AndroidBug5497Workaround(Activity activity, int svChildLayoutId) {
this.activity = activity;
svChildLayout = activity.findViewById(svChildLayoutId);
originalGravity = ((ScrollView.LayoutParams)svChildLayout.getLayoutParams()).gravity;
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)) {
// keyboard probably just became visible
onKeyboardVisible();
frameLayoutParams.height = usableHeightSansKeyboard - heightDifference;
} else {
// keyboard probably just became hidden
onKeyboardHidden();
frameLayoutParams.height = usableHeightSansKeyboard;
}
mChildOfContent.requestLayout();
usableHeightPrevious = usableHeightNow;
}
}
private int computeUsableHeight() {
Rect r = new Rect();
mChildOfContent.getWindowVisibleDisplayFrame(r);
return (r.bottom - r.top);
}
private void onKeyboardVisible() {
ScrollView.LayoutParams params = (ScrollView.LayoutParams) svChildLayout.getLayoutParams();
params.gravity = Gravity.TOP;
svChildLayout.requestLayout();
final ScrollView parentSv = (ScrollView) svChildLayout.getParent();
parentSv.post(new Runnable() {
@Override
public void run() {
View focusedEditText = activity.getWindow().getCurrentFocus();
parentSv.smoothScrollTo(0, focusedEditText.getTop());
}
});
}
private void onKeyboardHidden() {
ScrollView.LayoutParams params = (ScrollView.LayoutParams) svChildLayout.getLayoutParams();
params.gravity = originalGravity;
svChildLayout.requestLayout();
}
}
1
在試試這個AndroidMenifest
android:windowSoftInputMode="stateAlwaysHidden|adjustResize
在你editext使用本
android:inputType="textMultiLine|textPostalAddress"
android:scrollbars="vertical"
+0
不是你的問題解決?@Surendran –
相關問題
- 1. 安卓系統 - 當鍵盤彈出時調整片段大小
- 2. 安卓軟鍵盤大寫
- 3. 安卓調整圖像大小
- 4. 調整大小/位置帆布(安卓)
- 5. 安卓鍵盤
- 6. ipad鍵盤調整大小問題
- 7. uitableview不用鍵盤調整大小
- 8. Android鍵盤調整大小問題
- 9. 安卓鍵盤不能調整/平移工作android:windowSoftInputMode =
- 10. 安卓顯示/隱藏小工具調整大小填寫父
- 11. 安卓EDITTEXT鍵盤的setText()
- 12. 安卓軟鍵盤修改
- 13. 自定義安卓鍵盤
- 14. 安卓:setOnItemClickListener不軟鍵盤
- 15. 安卓鍵盤擱置
- 16. 安卓鍵盤輸入
- 17. 蘋果腳本:調整大小和位置虛擬鍵盤/鍵盤查看器
- 18. iOS 5中的鍵盤調整大小事件(中文鍵盤錯誤)?
- 19. 安卓鍵盤多字符鍵
- 20. Phonegap + onsoft鍵盤調整盤
- 21. 捕捉最小化,最大化,調整大小,鍵盤焦點變化等
- 22. 將widget的內容和佈局動態調整爲用戶通過調整大小定義的大小。安卓
- 23. 當鍵盤顯示時,活動沒有調整大小
- 24. 當鍵盤出現時調整UITextView的大小(Swift)
- 25. 當出現鍵盤時調整UITextView的大小
- 26. 當鍵盤彈出自動佈局時調整UITextView的大小
- 27. 調整ViewController的大小以容納鍵盤
- 28. 當Android鍵盤出現時統一調整大小屏幕
- 29. 如何在鍵盤顯示屏上調整AlertDialog的大小
- 30. Android軟鍵盤 - 調整大小圖標預覽
你試過[這個答案](http://stackoverflow.com/a/ 34563274/2826147) –