Rud*_*_TM 35
我们这样做了
myLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Rect r = new Rect();
parent.getWindowVisibleDisplayFrame(r);
int screenHeight = parent.getRootView().getHeight();
int heightDifference = screenHeight - (r.bottom - r.top);
Log.d("Keyboard Size", "Size: " + heightDifference);
}
});
Run Code Online (Sandbox Code Playgroud)
我们只用键盘调整视图大小,所以我们可以使用它.
FDI*_*DIM 21
Rect r = new Rect();
View rootview = this.getWindow().getDecorView(); // this = activity
rootview.getWindowVisibleDisplayFrame(r);
Run Code Online (Sandbox Code Playgroud)
结果是您的应用程序在屏幕上使用的空间量(即使未调整活动大小时也能正常工作).显然键盘将使用剩余的屏幕空间(如果可见)
在这里找到id:https://github.com/freshplanet/ANE-KeyboardSize/blob/master/android/src/com/freshplanet/ane/KeyboardSize/getKeyboardY.java
小智 9
如果您的活动不是全屏,请使用以下代码:
content.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
// TODO Auto-generated method stub
if (keyBoardHeight <= 100) {
Rect r = new Rect();
content.getWindowVisibleDisplayFrame(r);
int screenHeight = content.getRootView()
.getHeight();
int heightDifference = screenHeight
- (r.bottom - r.top);
int resourceId = getResources()
.getIdentifier("status_bar_height",
"dimen", "android");
if (resourceId > 0) {
heightDifference -= getResources()
.getDimensionPixelSize(resourceId);
}
if (heightDifference > 100) {
keyBoardHeight = heightDifference;
}
Log.d("Keyboard Size", "Size: " + heightDifference);
}
// boolean visible = heightDiff > screenHeight / 3;
}
});
Run Code Online (Sandbox Code Playgroud)
小智 5
如果要在活动大小不变(adjustPan)时计算虚拟键盘高度,则可以使用此示例:
https://github.com/siebeprojects/samples-keyboardheight
它使用隐藏窗口来计算窗口和活动的根视图之间的高度差.
归档时间: |
|
查看次数: |
47312 次 |
最近记录: |