【问题标题】:ConstraintLayout wrapped in ScrollView is still resized when keyboard is opened包裹在 ScrollView 中的 ConstraintLayout 在打开键盘时仍会调整大小
【发布时间】:2018-01-02 21:21:59
【问题描述】:

我希望能够在不调整布局大小的情况下打开键盘。我不想使用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


    【解决方案1】:

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

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

    据此,您的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,大小尚未初始化。 first 在这里是因为onPreDraw 不仅仅被android调用一次。这是我默认固定为0的类的变量。

    【讨论】:

    • 一切正常。我还编辑您的评论以澄清。
    • 不要忘记移除 preDraw 监听器(通常在 onPreDraw 监听器的第一行完成),否则 onPreDraw 也会不断被调用,因为 Android 总是在重绘帧。
    【解决方案2】:

    使用match_parent 作为ScrollView child 的大小并不理想。您应该使用固定大小或wrap_content。在您当前的设置中,ConstraintLayout 尝试将其高度设置为 ScrollView's 高度,这会根据是否显示键盘而改变。

    您可以尝试将您的ConstraintLayout 在屏幕上显示后立即设置为固定高度。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-14
      • 1970-01-01
      • 1970-01-01
      • 2019-01-17
      • 1970-01-01
      • 2017-05-03
      • 2019-11-17
      相关资源
      最近更新 更多