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

别说谁变了你拦得住时间么 提交于 2019-12-10 13:33:17

问题


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.


回答1:


Add this line in the manifest of your activity.

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



回答2:


Use this code It will be Helpful.

android:windowSoftInputMode="adjustNothing"



回答3:


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.



来源:https://stackoverflow.com/questions/35585538/adjust-the-layout-when-the-android-soft-keyboard-is-shown-or-hidden

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!