android - 使用全屏模式时检测软输入可见性和高度

标签 android android-layout

我已经阅读了有关此问题的多个帖子,解决方案是使用 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/

相关文章:

Android CTS logcat 失败

java - 为什么我的 ListView 中的项目不可点击?

android - 软键盘打开后,屏幕无法正确自行调整

android - recyclerView 能否在最后一行包含与其他项目不同的自定义项目?

Android:存储字符串对的好方法

android - 如何在另一个div中滚动一个div?

java - 无法从线程启动 Activity

android - 使用 fragment 共享过渡时返回过渡无法正常工作

android - ImageView 不居中对齐

android - android 程序中的 AlertDialog.Builder 显示错误