安卓 - 当键盘消失时留下一个空白的区域

8
我遇到了键盘问题。当键盘消失后,它占用的空间仍然保持空白,而其余布局不会调整。
正常屏幕: enter image description here 有键盘: enter image description here 键盘被取消: enter image description here 我从未见过这种情况,因此我甚至不确定从哪里开始查找。这在4.2.2和5.1上都会发生。
另一个重要信息是这是一个自定义LinearLayout,包含所有内容。也许与此有关。如果需要,我可以上传任何代码。
这是主要的布局文件外壳。
<com.edgetechlabs.app_v2.MasterLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<!-- Menu (Drawer)-->
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:visibility="visible"
    android:layout_height="match_parent"
    android:orientation="vertical" >
     <ScrollView
        android:id="@+id/activity_main_menu_listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/drawer_background"
        android:cacheColorHint="#00000000" >
     </ScrollView>
</LinearLayout>
<!-- Fragment Holder -->
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <!-- This is where fragment will show up -->
    <FrameLayout
        android:id="@+id/fragment_master"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </FrameLayout>
</LinearLayout>

这是我的清单文件。
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MasterActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

请同时发布您的代码。 - Anshul Tyagi
你是否正在监听并处理软键盘的打开/关闭操作? - Ankit Bansal
请同时发布您的清单文件。如果您使用 android:windowSoftInputMode="adjustResize",这将影响所有布局的布局调整,但会对半透明/透明的某些内容产生问题... - ecle
@AnkitBansal 这只是一个基本的键盘,没有进行任何花哨的打开/关闭操作。 - The4thIceman
@eee清单中没有关于windowSoftInputMode的内容。 "adjustResize"是默认设置吗? - The4thIceman
4个回答

15

我添加了

android:windowSoftInputMode="adjustPan"

在activity标签中添加了我的清单后,现在它可以工作了。我以前从未遇到过这种情况,我想我的自定义布局某种程度上干扰了键盘。

感谢@eee的评论,指引我找到了正确的方向。


1
我有类似的问题,而且这行代码对我没有起作用。 - Canberk Ozcelik
@CanberkÖzçelik,我已经有一段时间没有看过这段代码了,自那时以来,Android已经推出了两个新版本。因此,可能会有所改变。 - The4thIceman
1
是的,但如果有人遇到了类似的问题并且没有解决,我的问题在这里:https://dev59.com/_5_ha4cB1Zd3GeqP3LM1 - Canberk Ozcelik
我曾经遇到过同样的问题,但是是与“AppBarLayout”有关的。在寻找解决方案超过5个小时后,这个答案拯救了我。如果上面那个方法不起作用,请尝试这个:android:windowSoftInputMode="adjustNothing|adjustPan" - Gazzx
如果您想要调整大小,应该怎么做? - hmac

0
如果您的活动在AndroidManifest.xml中没有设置adjustPan,而是需要adjustResize,那么这是唯一帮助我解决白色空白区域问题的方法。
EditText.setOnFocusChangeListener { v, hasFocus ->
    if (hasFocus) {
        // something if needed
    } else {
        Utils.hideKeyboardToAdjustPan(activity)
    }
}

fun hideKeyboardToAdjustPan(activity: Activity?) {
    activity?.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN)
    hideKeyboard(activity)
}

fun hideKeyboard(activity: Activity?) {
    if (activity != null) {
        hideKeyboard(activity.currentFocus)
    }
}

fun hideKeyboard(view: View?) {
    if (view != null) {
        val inputManager = view.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        inputManager.hideSoftInputFromWindow(
            view.windowToken,
            WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED
        )
    }
}

0
尝试将ScrollView更改为androidx.core.widget.NestedScrollView 这帮助我解决了问题。

这个不起作用。 - Kebab Krabby
你也可以将CoordinatorLayout更改为NestedScrollView - CoolMind

0
在我的情况下,我有 android:windowSoftInputMode="adjustResize",但这并不重要。在一个布局中,当列表太短且键盘显示,然后隐藏时,列表占用的空间较小。列表下方会出现一个空白空间(键盘所在的位置)。
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        ... >

        <com.google.android.material.appbar.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <include
                layout="@layout/details"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_scrollFlags="scroll|exitUntilCollapsed|snap" />

        </com.google.android.material.appbar.AppBarLayout>

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="com.example.behavior.AutoSizeBehavior"> // Wrong class

            <androidx.recyclerview.widget.RecyclerView
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </FrameLayout>
    </androidx.coordinatorlayout.widget.CoordinatorLayout>

我尝试更改app:layout_scrollFlags="scroll|exitUntilCollapsed|snap",但它对滚动不好。然后我用NestedScrollView替换了CoordinatorLayout,它有所帮助,但布局发生了变化。之后我注意到app:layout_behavior引用了一个自定义类(有错误)。我将其更改为@string/appbar_scrolling_view_behavior。您还可以更改为com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接