Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save CodeK1988/2515e82959e6101bd2f49a2c3cbfb30b to your computer and use it in GitHub Desktop.
Save CodeK1988/2515e82959e6101bd2f49a2c3cbfb30b to your computer and use it in GitHub Desktop.
ConstraintLayout edittext keyboard bottom
遇到的问题
https://stackoverflow.com/questions/17630336/space-between-keyboard-and-edittext-in-android/25400043
1.edittext 试着加paddingBottom 是有用的 但是edittext字体sp != dp 不太好写布局
2.stateHidden|adjustResize 或者 adjustPan 选一种 前者是页面允许压缩 但是我的页面比较紧凑不太合适 所以只有用 adjustPan
3.接着想是不是监听键盘展开收起 试着ConstraintLayout外层添加滑动布局让其滑动到一定的高度呢?
ScrollView scrollto (0,height) 不管用 试了很久 忽然想到 是不是ScrollView android:layout_height="match_parent" 导致的?
又尝试了很久。。。
最终解决方式 监听键盘展开收起+ScrollView paddingBottom
kotlin 代码如下
scrollView.viewTreeObserver.addOnGlobalLayoutListener(this)
override fun onGlobalLayout() {
try {
val r = Rect()
scrollView.getWindowVisibleDisplayFrame(r)
if (Math.abs(scrollView.rootView.height - (r.bottom - r.top)) > 100) { // if more than 100 pixels, its probably a keyboard...
scrollView?.setPadding(0, 0, 0, dip(100))
} else {
scrollView?.setPadding(0, 0, 0, 0)
}
} catch (e: Exception) {
e.printStackTrace()
CrashReport.postCatchedException(e)
}
}
override fun onDestroy() {
super.onDestroy()
try {
scrollView.viewTreeObserver.removeOnGlobalLayoutListener(this)
} catch (e: Exception) {
e.printStackTrace()
CrashReport.postCatchedException(e)
}
}
xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/shape_gradient_game">
<ScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:fillViewport="true"
android:focusable="true"
android:focusableInTouchMode="true">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom">
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment