【问题标题】:There is a issue with constraint layout with RecyclerViewRecyclerView 的约束布局存在问题
【发布时间】:2019-08-30 16:37:51
【问题描述】:

这是我用于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>

这是我得到的输出

当我将其更改为 Linear layoutrelative layout 时,它工作正常

带有线性布局代码

    <?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>

这是我使用上述代码时的输出

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

【问题讨论】:

  • 分享 RecyclerView 项目的自定义布局
  • 当我使用linearlayout 时效果很好
  • 分享你的适配器或它的布局
  • 我刚刚编辑了它
  • 您应该使用 Ctrl+K 或 ⌘+K 来正确格式化您的源代码块。

标签: android android-constraintlayout


【解决方案1】:

如果您在constraintlayout 中设置相对于父级的视图,则必须使用宽度和高度作为 0dp。

请注意,“MATCH_PARENT 不建议用于包含在 ConstraintLayout 中的小部件。类似的行为可以通过使用 MATCH_CONSTRAINT 来定义,并将相应的左/右或上/下约束设置为父级”在文档中开发者网站。

试试这个布局。这应该对你有帮助。

<?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
  • 正如我在上面添加的解释,现在建议在约束布局中避免匹配父视图。约束布局子视图应使用 dp 中的值,以便所有屏幕尺寸的计算都很容易
  • 在计算时添加匹配父项不会为您提供特定的大小
  • 是的,我可以帮助你
猜你喜欢
  • 2019-06-20
  • 2023-03-10
  • 1970-01-01
  • 2015-04-06
  • 2019-01-06
  • 1970-01-01
  • 2020-10-12
  • 1970-01-01
相关资源
最近更新 更多