gpt4 book ai didi

android - 当键盘打开时,包裹在 ScrollView 中的 ConstraintLayout 仍然会调整大小

转载 作者:搜寻专家 更新时间:2023-11-01 07:44:13 25 4
gpt4 key购买 nike

我希望能够在不调整布局大小的情况下打开键盘。我不想使用 adjustNothing,因为我需要使用键盘调出底部 View 之一。我也不想使用 adjustPan,因为这会使整个布局离开屏幕,这有点难看。

因此,我将我的布局 (ConstraintLayout) 包裹在 ScrollView 中,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<data>
<variable
name="viewModel"
type="com.mypackage.MyViewModel"/>
</data>

<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:scrollbars="none">

<!-- I don't want anything in this ConstraintLayout to move or resize when I open the keyboard, hence why I wrapped it in a ScrollView. -->
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<FrameLayout
android:id="@+id/scene_container"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/guideline"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">

<ImageView
android:id="@+id/scene"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:adjustViewBounds="true"
android:background="@color/black"
android:scaleType="fitCenter"
android:src="@drawable/grumpy"/>
</FrameLayout>

<android.support.constraint.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent=".7"/>

<View
android:id="@+id/status_bar"
android:layout_width="0dp"
android:layout_height="48dp"
android:background="@color/colorPrimary"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/guideline"/>

<android.support.v7.widget.RecyclerView
android:id="@+id/my_recycler"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="48dp"
android:clipToPadding="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/status_bar"/>

</android.support.constraint.ConstraintLayout>

</ScrollView>

<!-- The bottom sheet contains a 48dp high peekable area (a bar with an EditText) that I need to raise with the keyboard. -->
<include
android:id="@+id/bottom_sheet"
layout="@layout/bottom_sheet"
app:viewModel="@{viewModel}"/>

</android.support.design.widget.CoordinatorLayout>

这在我调出键盘的前两次工作正常。这是我遇到的奇怪问题。每 三次 调出键盘时,我的布局都会调整大小并挤压在一起,就好像 ScrollView 根本不存在一样。我每次三次打开键盘时都会出现这种情况,如下所示:

1) Open keyboard. Layout remains same size.
Close keyboard.
2) Open keyboard. Layout remains same size.
Close keyboard.
3) Open keyboard. *LAYOUT RESIZES/SQUISHES TOGETHER!*
Close keyboard.

The above cycle repeats as I continue to open/close the keyboard.

有什么解决办法吗?为什么每三次打开键盘时布局都会调整一次?

最佳答案

实际上,如果您使用 match_parent 作为 ScrollView child 的大小,它也会在键盘出现时调整大小并适合 ScrollView 大小.

要了解 ScrollView 的工作原理,您可以考虑浏览器。在您访问的每个链接上,如果页面不适合您的浏览器窗口,您将看到滚动条以让您在页面中导航。ScrollView 都是一样的,你只需要给他们你想要在屏幕上占据的大小(比如浏览器窗口)就可以把所有东西都放在里面(比如网站页面),即使是大于 ScrollView 大小。

据此,您的 ConstraintLayout 需要固定大小。如果您只希望用户访问其中的所有组件,它可以是 wrap_content。这不是你的情况。你希望它保持他的原始大小,也就是说,正如我在你的代码中看到的那样,也是你的第一个 CoordinatorLayout 的大小。

我发现将内部 ConstraintLayout 固定为原始 View 大小的唯一方法是通过代码。

让我们简化您的 xml 示例:

<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:id="@+id/parentView"
android:layout_height="match_parent">

<ScrollView
android:id="@+id/scrollViewItem"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.constraint.ConstraintLayout
android:id="@+id/childView"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<!--ALL YOUR ITEMS-->

</android.support.constraint.ConstraintLayout>

</ScrollView>

</android.support.design.widget.CoordinatorLayout>

您需要在加载屏幕时修复 childView 的大小。在 onCreate 上,您可以添加以下代码:

private int first = 0;

public class ConstaintLayoutCanScroll extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

first = childView.getMeasuredHeight();

final CoordinatorLayout parentView = findViewById(R.id.parentView);
parentView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
if (first < parentView.getMeasuredHeight()) {
ConstraintLayout childView = findViewById(R.id.childView);
childView.setMinHeight(parentView.getMeasuredHeight());
first = childView.getMeasuredHeight();
}
return true;
}
});

}

}

我使用监听器是因为在 OnCreate 中,大小尚未初始化。first 在这里是因为 onPreDraw 不是只被 android 调用一次。它是类的一个变量,我默认固定为 0。

关于android - 当键盘打开时,包裹在 ScrollView 中的 ConstraintLayout 仍然会调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48068006/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com