我知道我可以使用机器人:windowSoftInputMode =adjustPan做一些自动偏移但我真的想要得到的软件键盘高度,一些特殊用途
I know I can use "android:windowSoftInputMode="adjustPan" to do some automatically offset. But I really want to get software keyboard height for some special purpose.
我发现这里有一个类似的话题:Getting软键盘的尺寸。但很明显,它不是一个通用的解决方案。
I got to find that there's a similar topic here: Getting the dimensions of the soft keyboard. But obviously, it's not an universal solution.
有没有一种通用的方式或内置的方法来获取软键盘高度? (或者如何才能得到当前光标所在位置和软件键盘顶部位置之间的偏移值?)
Is there a common way or built-in method to get soft keyboard height? (or how can I get the offset value between current cursor position and software keyboard top position?)
非常感谢
推荐答案
下面我的解决方案,这也是哈克,但解决问题。
Here my solution, it is also hacky but solve the problem.
我有与在屏幕的底部透明背景临时视图的地方。我已经加入的android:windowSoftInputMode =adjustResize
旗活动标记像@bill清单显示 现在主要的故事是 onGlobalLayout()
。在那里,我计算出的临时视图的 Y轴
和高度根视图
I have places on temporary view with transparent background at the bottom of the screen.
I have added android:windowSoftInputMode="adjustResize"
flag in activity tag in manifest like @bill suggests.
Now main story is in onGlobalLayout()
. There i calculate the difference between the y axis
of temp view and height of root view
final View view = findViewById(R.id.base);
view.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
int rootViewHeight = view.getRootView().getHeight();
View tv = findViewById(R.id.temp_view);
int location[] = new int[2];
tv.getLocationOnScreen(location);
int height = (int) (location[1] + tv.getMeasuredHeight());
deff = rootViewHeight - height;
// deff is the height of soft keyboard
}
});
相关推荐
最新文章