获取Android中虚拟键盘的高度
如何获取Android中虚拟键盘的高度?是否可以?
我尝试从主窗口获取它,但它给了我应用程序的完整高度。但我想得到键盘的高度。
How can I get the height of virtual keyboard in Android? Is it possible?
I try to get it from the main window, but it gives me full height of the application. But I want to get the keyboard height.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您无法获取键盘高度,但可以获取 View 的高度,这是您真正想要的 - 并且您将获得提供给当前视图的 onLayout 调用的数据。
You can't get the keyboard height, but you can get the height of your View, which is what you really want - and you'll get this data supplied to the onLayout call into the current view.
您可以使用此示例代码。这是肮脏的解决方案,但它有效
you can use this sample code. it is dirty solution but it works
这个解决方案也很hacky,但解决了问题(至少对我来说)。
android:windowSoftInputMode="adjustResize"
标志。现在主要故事在
onGlobalLayout()
中。我计算了临时视图的 y 轴和根视图的高度之间的差异最终视图视图 = findViewById(R.id.base);
view.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
<前><代码>@Override
公共无效onGlobalLayout(){
int rootViewHeight = view.getRootView().getHeight();
查看电视 = findViewById(R.id.temp_view);
int location[] = new int[2];
tv.getLocationOnScreen(位置);
int height = (int) (location[1] + tv.getMeasuredHeight());
def = rootViewHeight - 高度;
// def 是软键盘的高度
}
});
This solution is also hacky but solve the problem (atleast for me).
android:windowSoftInputMode="adjustResize"
flag in activity tag in manifest.Now main story is in
onGlobalLayout()
. There i calculate the difference between the y axis of temp view and height of root viewfinal View view = findViewById(R.id.base);
view.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
});
如果您不想在应用中使用
android:windowSoftInputMode="adjustResize"
。你可以尝试这样的事情:
If you don't want
android:windowSoftInputMode="adjustResize"
in your app.You can try something like this:
假设布局的根视图是RelativeLayout。
您可以做什么创建一个 CustomRelativeLayout 类,它扩展了RelativeLayout 并
重写其中的 onSizeChanged 方法。
所以当软键盘打开时,RelativeLayout的高度就会改变,
更改将在 onSizeChanged 方法内通知。
在清单文件中进行这些更改,以便活动以调整大小模式打开,而不是平移和扫描模式。在调整大小模式下,键盘打开时布局将能够调整大小。
要了解有关全景扫描和调整大小的更多信息,请访问
https://developer.android.com/training/keyboard-input/visibility
Let's suppose that the rootView of your layout is RelativeLayout.
What you can do create a class CustomRelativeLayout which extends RelativeLayout and
Overrides onSizeChanged Method inside it.
So when the soft keyboard opens up, the height of the RelativeLayout will change and the
change will be notified inside the onSizeChanged Method.
In your Manifest file make these changes so that the Activity opens in Resize Mode and Not pan and scan mode. In Resize mode the Layout will be able to resize when keybaord opens up.
To read more about pan-scan and Resize visit
https://developer.android.com/training/keyboard-input/visibility
试试这个
Try this