打开键盘时,仍然会调整ScrollView中包含的ConstraintLayout的大小

问题描述 投票:0回答:2

我希望能够在没有调整大小的情况下打开键盘。我不想使用adjustNothing,因为我需要一个底部视图来提升键盘。我也不想使用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.

任何想法如何解决这个问题?为什么每次打开键盘时布局会调整大小?

android android-scrollview android-coordinatorlayout android-constraintlayout
2个回答
3
投票

实际上,如果你使用match_parent作为ScrollView孩子的大小,当键盘出现并符合ScrollView大小时,它也会调整大小。

要了解ScrollView的工作原理,您可以考虑使用浏览器。在你去的每个链接上,如果页面不适合你的浏览器窗口,你会看到滚动条让你在页面中导航。 ScrollView是相同的,你只需要给它们你想要在屏幕上占据的大小(如浏览器窗口),你可以把所有内容(如网站页面),即使它大于ScrollView大小。

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

我发现将内部ConstraintLayout修复为原始视图大小的唯一方法是通过代码。

让我们为例子简化你的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,大小不是init。 first在这里因为onPreDraw不是只被android调用一次。这是我默认修复为0的类的变量。


1
投票

使用match_parent作为ScrollView孩子的大小并不理想。你应该使用固定大小或wrap_content。在您当前的设置中,ConstraintLayout尝试将其高度设置为ScrollView's高度,并根据显示或不显示的键盘进行更改。

您可以尝试将ConstraintLayout设置为固定高度,只要它在屏幕上显示即可。

要测试该方法是否有效,您可以尝试设置例如你的android:layout_height="400dp"上的ConstraintLayout,看看你提到的“周期”是否会发生。如果可行,您可以继续使用与屏幕大小匹配的ConstraintLayout的实际尺寸。

© www.soinside.com 2019 - 2024. All rights reserved.