【问题标题】:Row item of recycle view in Constraint layout collapse instead taking parent widthConstraintlayout中recyclerview的行项折叠而不是采用父宽度
【发布时间】:2018-05-16 07:49:06
【问题描述】:

最近,我开始使用约束布局,但今天遇到了非常奇怪的行为。当我在 fragment_holiday.xml 中使用 RelativeLayout 作为根而不是 ConstraintLayout 时,输出符合预期,但我想知道 ConstraintLayout 有什么问题。我的代码片段如下,item_row

我得到约束布局的输出(并且在滚动时显示不同的行为,正如您在我分享的屏幕截图中看到的那样)---

fragment_holiday.xml

    <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.thepsi.psidashboard.Fragments.HolidayFragment">

    <com.thepsi.psidashboard.CustomViews.CustomDatePicker
        android:id="@+id/customDatePicker_holiday"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:dateFormat="MONTH_YEAR"/>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycle_view_holiday"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/general_margin_eight_dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/customDatePicker_holiday" />
</android.support.constraint.ConstraintLayout>

item_holiday.xml

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="wrap_content">

    <TextView
        android:id="@+id/textView25"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:text="24 - January - 2017"
        android:textColor="@color/colorPrimaryDark"
         />

    <TextView
        android:id="@+id/textView26"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:text="Saturday"
        android:layout_alignParentEnd="true"
        android:textColor="@color/colorPrimaryDark"
        />

    <TextView
        android:id="@+id/textView27"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorAccent"
        android:text="Mahavir Jayanti"
        android:textAlignment="center"
        android:textColor="@android:color/black"
        android:layout_below="@+id/textView25"
        />
</RelativeLayout>

预期设计 -

有人可以告诉我我在这里缺少什么吗?

【问题讨论】:

  • 检查回收站视图的宽度。给“matchparent”
  • @ArnoldBrown 是的,它有效!!!!!!!但是为什么不建议将 MATCH_PARENT 用于 ConstraintLayout 中包含的小部件的文档中。可以使用 MATCH_CONSTRAINT 定义类似的行为

标签: android android-recyclerview android-constraintlayout


【解决方案1】:

试试这个...

recyclerviewwidth改为ma​​tchparent

是的!根据文档,ConstraintLayout 中的 widget 不支持将 ma​​tchparent 作为 width

我们必须将 width 指定为 0dp 并使用 constraintStart_toStartOf="parent"constraintEnd_toEndOf="parent" 以反映您使用的 ma​​tchparent

但我给出的解决方案将适用于您的某些情况。

【讨论】:

    【解决方案2】:

    尝试更新 item_holiday.xml,如下面的 xml。我刚刚添加了三个标签并添加了它们的约束属性。 您将需要根据您的要求编辑标签属性。

    <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="wrap_content">
    
    
        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:layout_marginEnd="88dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="32dp"
            android:text="TextView"
            app:layout_constraintEnd_toStartOf="@+id/textView2"
            app:layout_constraintHorizontal_bias="0.888"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:layout_marginEnd="88dp"
            android:layout_marginTop="32dp"
            android:text="TextView"
            app:layout_constraintEnd_toStartOf="@+id/textView3"
            app:layout_constraintTop_toTopOf="parent" />
    
        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:layout_marginEnd="16dp"
            android:layout_marginTop="32dp"
            android:text="TextView"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </android.support.constraint.ConstraintLayout>
    

    【讨论】:

    • 感谢您的回复,但我不希望 View 具有您给定的固定尺寸。我正在用预期的设计更新我的答案。
    【解决方案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"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <TextView
            android:id="@+id/textView25"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="24 - January - 2017"
            android:textColor="@color/colorPrimaryDark"
            app:layout_constraintEnd_toStartOf="@+id/textView26"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <TextView
            android:id="@+id/textView26"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:gravity="right"
            android:text="Saturday"
            android:textColor="@color/colorPrimaryDark"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintLeft_toRightOf="@+id/textView25"
            app:layout_constraintTop_toTopOf="@+id/textView25" />
    
        <TextView
            android:id="@+id/textView27"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="@color/colorAccent"
            android:text="Mahavir Jayanti"
            android:textAlignment="center"
            android:textColor="@android:color/black"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView25" />
    </android.support.constraint.ConstraintLayout>
    

    【讨论】:

    • 感谢您的回复。没有成功 :( 现在看起来像 this.
    【解决方案4】:

    我在回收站视图中遇到了类似的高度和垂直约束问题,最终使用新的支持库版本 27.0.2 和约束布局版本 1.1.0-beta4 来获得预期的行为。 如果在约束布局中有一些奇怪的行为,建议大家尝试使用这些版本。

    【讨论】:

      猜你喜欢
      • 2018-12-23
      • 2017-03-01
      • 1970-01-01
      • 2014-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多