隐藏键盘

时间:2017-07-13 19:09:37

标签: android android-layout android-softkeyboard android-gridlayout

我的Android片段使用支持GridLayout来显示等间距视图(第一张图片)。在我的清单中,我正在改变软键盘的显示方式,以便在我点击EditText并显示键盘时调整GridLayout的大小(第二张图片):

android:windowSoftInputMode="adjustResize|stateHidden"

当隐藏软键盘时,GridLayout不会重置回其初始大小(最后一张图像)。

Screenshot showing full screen Screenshot with soft keyboard open Screenshot with soft keyboard closed and GridLayout no longer looking like first screenshot

如何让GridLayout在第一个屏幕截图中回到原来的配置?我知道如果从清单中删除android:windowSoftInputMode问题就会消失,但这不是我可以在另一个创建此演示的应用程序中更改的内容。

这是片段的布局文件:

<android.support.v7.widget.GridLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:grid="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    grid:columnCount="2"
    grid:rowCount="3"
    grid:orientation="horizontal">

    <EditText
        android:layout_width="100sp"
        android:layout_height="80sp"
        grid:layout_gravity="center"
        grid:layout_columnWeight="1"
        grid:layout_rowWeight="1"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="80sp"
        grid:layout_gravity="center"
        grid:layout_columnWeight="1"
        grid:layout_rowWeight="1"
        android:text="B"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="80sp"
        grid:layout_gravity="center"
        grid:layout_columnWeight="1"
        grid:layout_rowWeight="1"
        android:text="C"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="80sp"
        grid:layout_gravity="center"
        grid:layout_columnWeight="1"
        grid:layout_rowWeight="1"
        android:text="D"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="80sp"
        grid:layout_gravity="center"
        grid:layout_columnWeight="1"
        grid:layout_rowWeight="1"
        android:text="E"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="80sp"
        grid:layout_gravity="center"
        grid:layout_columnWeight="1"
        grid:layout_rowWeight="1"
        android:text="F"/>

</android.support.v7.widget.GridLayout>

2 个答案:

答案 0 :(得分:1)

显然GridLayout.Axis.layout()中有一个错误(在onLayout()中被称为)

如果确实需要使用GridLayout来调用requestLayout来激发其私有方法invalidateStructure()似乎可以解决此问题。

class FixedGridLayout : GridLayout {
    constructor(context: Context, attrs: AttributeSet?, defStyle: Int) : super(context, attrs, defStyle)
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
    constructor(context: Context) : super(context)

    override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
        super.onLayout(changed, left, top, right, bottom)
        if (changed) {
            post {
                requestLayout()
            }
        }
    }
}

答案 1 :(得分:0)

我放弃了GridLayout并切换到使用ConstraintLayout,它没有任何问题。

<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

        <EditText
            android:id="@+id/itemA"
            android:layout_width="100sp"
            android:layout_height="80sp"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintLeft_toLeftOf="@+id/itemC"
            app:layout_constraintBottom_toTopOf="@+id/itemC"
            app:layout_constraintRight_toRightOf="@+id/itemC"/>

        <TextView
            android:id="@+id/itemB"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="80sp"
            app:layout_constraintLeft_toLeftOf="@+id/itemD"
            app:layout_constraintBottom_toBottomOf="@+id/itemA"
            app:layout_constraintRight_toRightOf="@+id/itemD"
            app:layout_constraintVertical_chainStyle="spread"
            android:text="B"/>

        <TextView
            android:id="@+id/itemC"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="80sp"
            app:layout_constraintTop_toBottomOf="@+id/itemA"
            app:layout_constraintBottom_toTopOf="@+id/itemE"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toLeftOf="@+id/itemD"
            app:layout_constraintHorizontal_chainStyle="spread"
            android:text="C"
            app:layout_constraintVertical_bias="0.0"/>

        <TextView
            android:id="@+id/itemD"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="80sp"
            app:layout_constraintLeft_toRightOf="@+id/itemC"
            app:layout_constraintBottom_toBottomOf="@+id/itemC"
            app:layout_constraintRight_toRightOf="parent"
            android:text="D"/>

        <TextView
            android:id="@+id/itemE"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="80sp"
            app:layout_constraintTop_toBottomOf="@+id/itemC"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintRight_toLeftOf="@+id/itemF"
            android:text="E"/>

        <TextView
            android:id="@+id/itemF"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="80sp"
            app:layout_constraintTop_toBottomOf="@+id/itemD"
            app:layout_constraintLeft_toRightOf="@+id/itemE"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintRight_toLeftOf="parent"
            android:text="F"/>

    </android.support.constraint.ConstraintLayout>