首页 > 解决方案 > SwipeRefreshLayout 不遵守约束

问题描述

我有一个 SwipeRefreshLayout,其中包含一个用于聊天活动的 RecyclerView。我将其底部限制在屏幕底部的线性布局的顶部:

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/colorPrimaryDark"
    android:paddingLeft="0dp"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingRight="0dp"
    android:paddingBottom="0dp"
    tools:context="org.andrewedgar.theo.ChatRoomActivity">

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swiperefresh"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/listFooter"
        app:layout_constraintBottom_toTopOf="@+id/listFooter"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/chatRecyclerView"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_alignParentStart="true"
            android:layout_centerVertical="true"
            app:layout_constraintBottom_toTopOf="@+id/listFooter"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.0" />
    </android.support.v4.widget.SwipeRefreshLayout>

    <LinearLayout
        android:id="@+id/listFooter"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_alignParentBottom="true"
        android:background="@drawable/custom_border"
        android:gravity="bottom"
        android:orientation="horizontal"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">

        <EditText
            android:id="@+id/messageInput"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@android:color/transparent"
            android:ellipsize="start"
            android:gravity="center"
            android:hint="@string/prompt_msg"
            android:imeActionLabel="@string/action_send"
            android:imeOptions="actionUnspecified"
            android:inputType="textCapSentences|textAutoCorrect"
            android:maxLines="2"
            android:textColor="@color/white"
            android:textColorHint="@color/hintColor"
            android:importantForAutofill="no" tools:targetApi="o"/>

        <ImageButton
            android:id="@+id/sendButton"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="@android:color/transparent"
            android:contentDescription="@string/action_send"
            android:padding="10dp"
            android:src="@android:drawable/ic_menu_send" />


    </LinearLayout>


</android.support.constraint.ConstraintLayout>

..但 SwipeRefreshLayout 仍然占据整个屏幕,并且消息出现在消息输入后面:

在此处输入图像描述

是否需要特殊类型的约束才能使其起作用?

标签: javaandroidandroid-studio

解决方案


将高度更改SwipeRefreshLayout为 0dp

<android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/swiperefresh"
    android:layout_width="match_parent"
    android:layout_height="0dp"

为什么?

根据文档

如果至少有一个视图尺寸设置为“匹配约束”(0dp),您可以将视图尺寸设置为 16:9 等比例。

注意:您不能match_parent用于ConstraintLayout. 而是使用“ match constraints”(0dp)。


推荐阅读