问题描述
我的Android片段正在使用支持 GridLayout 显示等距视图(第一个图像).在清单中,我正在更改软键盘的显示方式,以便在我点击EditText并显示键盘时调整GridLayout的大小(第二张图片):
My Android fragment is using the support GridLayout to display equally-spaced views (first image). In my manifest I'm changing how the soft keyboard displays so it resizes the GridLayout when I tap on the EditText and the keyboard is displayed (second image):
android:windowSoftInputMode="adjustResize|stateHidden"
隐藏软键盘后,GridLayout不会重置回其初始大小(最后一张图像).
When the soft keyboard is hidden, the GridLayout does not reset back to its initial size (last image).
在第一个屏幕截图中,关于如何使GridLayout返回其原始配置的任何想法吗?我知道如果我从清单中删除了android:windowSoftInputMode
,问题就消失了,但这不是我可以在创建此演示的另一个应用程序中进行的更改.
Any idea on how to get the GridLayout to go back to its original configuration in the first screenshot? I know if I remove the android:windowSoftInputMode
from the manifest the problem goes away, but that is not something I can change in another app from which this demo is created.
这是片段的布局文件:
<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>
推荐答案
显然在GridLayout.Axis.layout()
中有一个错误(在onLayout()
中称为)
Apparently there is a bug in GridLayout.Axis.layout()
(which is called in onLayout()
)
如果确实需要使用GridLayout
调用requestLayout
来触发其私有方法invalidateStructure()
似乎可以解决此问题.
If you indeed need to use GridLayout
calling requestLayout
to fire its private method invalidateStructure()
seems to address the problem.
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()
}
}
}
}
这篇关于从隐藏键盘调整大小后,GridLayout不会重置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!