我已经阅读了有关此问题的多个帖子,解决方案是使用 OnGlobalLayoutListener
。这里的问题是我的应用程序在 list 中声明了以下内容:
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
所以输入模式调整不起作用,我不能使用 OnGlobalLayoutListener
如何获得软输入可见/消失的事件,我还需要获得输入的高度以及调整我当前的布局 bottomMargin
。
谢谢。
最佳答案
我不知道你如何使用 OnGlobalLayoutListener
但它在我下面的代码中工作正常:
final View activityRootView = findViewById(R.id.container);
final ViewTreeObserver observer = activityRootView
.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Rect r = new Rect();
// r will be populated with the coordinates of your view
// that area still visible.
activityRootView.getWindowVisibleDisplayFrame(r);
int heightDiff = activityRootView.getRootView().getHeight()
- (r.bottom - r.top);
Toast.makeText(TestActivity.this, String.valueOf(heightDiff),
Toast.LENGTH_SHORT).show();
}
});
上面的代码在我的 TestActivity 的 onResume()
中,主题是 @android:style/Theme.NoTitleBar.Fullscreen
View 的布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/textview"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="aaa" />
</LinearLayout>
list 中的 Activity 配置:
<activity
android:name=".ui.activity.TestActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
关于android - 使用全屏模式时检测软输入可见性和高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15923447/