Adjust the layout when the Android soft keyboard is shown or hidden

My goal is to achieve what most messaging apps like Facebook messenger, Viber, What's up do when displaying an attachment sheet. If the Soft keyboard is visible and the user wants to attach something, the keyboard is hidden and the attachment sheet is presented in its place.

In order to make this work, the layout changes should happen when the root view size changes. Otherwise, a graphical glitch occurs where my layout changes are applied shortly before keyboard is shown/hidden.

If I could change my layout the exact moment the keyboard is hidden, I could get it right. I have tried using onGlobalLayoutListener but without the desired outcome.

在这里输入图像描述


将此行添加到您的活动清单中。

 <activity
        android:name="com.your.Activity"
        android:windowSoftInputMode="stateHidden|adjustPan" />

使用此代码这将是有帮助的。

android:windowSoftInputMode="adjustNothing"

The best place to find out whether your layout changed due to the keyboard (dis)appearing and acting on the change is inside onMeasure() of the root layout.

In more detail, I made a custom LinearLayout, which is used as my root layout. I overrided the onMeasure and I calculate what is the current keyboard height. Depending on the keyboard's height and whether my attachment grid should be visible or not, I change the visibility of the attachment grid. I can even set the grid's height the same as the keyboard.

The results looks like the grid was always beneath the keyboard and the textview remains in the same vertical place.

链接地址: http://www.djcxy.com/p/93434.html

上一篇: 软键盘在Android中打开时如何排列布局

下一篇: 显示或隐藏Android软键盘时调整布局