ConstraintLayout中的RecyclerView无法正确显示 [英] RecyclerView in ConstraintLayout is not showing properly

查看:72
本文介绍了ConstraintLayout中的RecyclerView无法正确显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过链接,指南 Barrier 进行了此 xml 的测试,但它们都无济于事.

I tested this xml with chaining, Guideline and Barrier but none of them could help.

xml 代码:

<?xml version="1.0" encoding="utf-8"?>
<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="match_parent"
    android:layout_height="match_parent"
    tools:context=".RatingActivity">

    <TextView
        android:id="@+id/welcomeTextView"
        android:layout_width="0dp"
        android:layout_height="74dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="79dp"
        android:gravity="clip_vertical|center_horizontal"
        android:text="@string/welcome"
        android:textAppearance="@style/TextAppearance.AppCompat.Medium"
        android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@+id/recyclerView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="0dp"
        android:layout_height="467dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="24dp"
        android:background="@android:color/darker_gray"
        android:padding="4dp"
        android:scrollbars="vertical"
        app:layout_constraintBottom_toTopOf="@+id/submitButton"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/submitButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        android:text="@string/submit"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</android.support.constraint.ConstraintLayout>

AVD 上运行时, RecyclerView 占用的空间超过了所需的空间,从而导致覆盖或退回 welcomeTextView submitButton .

When it's run on an AVD, the RecyclerView takes more space than what is needed causing to cover or push back either welcomeTextView or submitButton.

顺便说一句,这是 Gradle 依赖项:

BTW, this is the Gradle dependency:

implementation 'com.android.support.constraint:constraint-layout:1.1.3'

推荐答案

尝试:

<?xml version="1.0" encoding="utf-8"?>
<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="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/welcomeTextView"
        android:layout_width="0dp"
        android:layout_height="74dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="79dp"
        android:gravity="clip_vertical|center_horizontal"
        android:text="welcome"
        android:textAppearance="@style/TextAppearance.AppCompat.Medium"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="24dp"
        android:background="@android:color/darker_gray"
        android:padding="4dp"
        android:scrollbars="vertical"
        app:layout_constrainedHeight="true"
        app:layout_constraintTop_toBottomOf="@+id/welcomeTextView"
        app:layout_constraintBottom_toTopOf="@id/submitButton" />

    <Button
        android:id="@+id/submitButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        android:text="submit"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</android.support.constraint.ConstraintLayout>

唯一的更改是将RecyclerView的顶部设置为文本的底部,然后将RecyclerView的高度设置为0,然后将 app:layout_constrainedHeight ="true" 设置为启用高度计算基于约束.如果欢迎消息的下边距不起作用,请尝试为RecyclerView设置上边距.

The only change is set the top of your RecyclerView instead the bottom of your text, and then set the RecyclerView height to 0 and set the app:layout_constrainedHeight="true" to enable the height calculation based on constraints. If the welcome message bottom margin doesn't work, try to set a top margin to the RecyclerView.

这篇关于ConstraintLayout中的RecyclerView无法正确显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆