ConstraintLayout与RecyclerView存在问题。

4

这是我用于 ConstraintLayout 的代码

<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="com.lokeshlabs.filterableexample.MainActivity">

<android.support.v7.widget.RecyclerView
    android:id="@+id/rv_movie"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"/>

</android.support.constraint.ConstraintLayout>

这是布局项

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="?attr/listPreferredItemHeight"
    android:layout_margin="4dp"
    android:weightSum="100"
    android:orientation="horizontal">

<TextView
    android:id="@+id/tv_movie_name"
    android:layout_width="0dp"
    android:layout_weight="80"
    android:layout_height="match_parent"
    android:hint="movie name"
    android:textColor="@android:color/black"
    android:textSize="22sp" />


<TextView
    android:id="@+id/tv_year"
    android:layout_width="0dp"
    android:layout_weight="20"
    android:layout_height="match_parent"
    android:hint="year"
    android:padding="8dp"
    android:gravity="end"
    android:textColor="@android:color/black"
    android:textSize="18sp"/>
</LinearLayout>

这是我得到的输出

Here is the output i got

当我将此更改为线性布局相对布局时,它可以正常工作。
使用线性布局代码。
    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="com.lokeshlabs.filterableexample.MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_movie"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

使用上述代码时的输出如下:

enter image description here

甚至尝试了constraintlayout中的所有方法,但没有用,可能是一个bug。


3
分享您的RecyclerView项的自定义布局。 - Goku
当我使用LinearLayout时,它的工作效果很好。 - Naga Lokesh Kannumoori
1
分享你的适配器或其布局。 - Payal Sorathiya
我刚刚编辑了它。 - Naga Lokesh Kannumoori
你应该使用Ctrl+K或⌘+K来正确格式化你的源代码块。 - Eugene Brusov
1个回答

5

如果你要将视图相对于父视图设置在constraintlayout中,必须使用0dp作为宽度和高度。请注意,在开发者文档中指出“不建议在ConstraintLayout中使用MATCH_PARENT。可以通过将相应的左/右或上/下约束设置为parent来定义类似的行为”,请留意。

尝试使用这个布局。这应该会有所帮助。

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_movie"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"/>

</android.support.constraint.ConstraintLayout>

这很有用,请问您能告诉我为什么一定要使用“0dp”吗? - Naga Lokesh Kannumoori
正如我在上面所解释的,现在建议在约束布局中避免使用match parent。约束布局的子视图应该使用dp值,这样在所有屏幕尺寸上的计算都会更容易。 - Sharath kumar
是的,我可以帮助您。 - Sharath kumar
让我们在聊天中继续这个讨论 - Naga Lokesh Kannumoori
@Anonymous 你能否看一下这个问题 here,它与你的问题有些相似。 - pallav bohara
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接