在ConstraintLayout中使用链时,视图宽度变为零

时间:2018-05-30 13:04:16

标签: android android-layout user-interface android-constraintlayout

我有两个TextView水平链接并显示在DialogFragment中。动态设置的两个TextView可见性,有时tvA将是可见的,有时tvB或两者都会显示。

  

这是我的xml

<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:background="@color/app_background_color"
android:minWidth="250dp">

<TextView
    android:id="@+id/tvElement"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:background="@drawable/shape_back"
    android:gravity="center"
    android:padding="10dp"
    android:text="@string/elements"
    android:textColor="@color/item_view_color"
    app:layout_constraintEnd_toStartOf="@+id/tvSensor"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/tvSensor"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:background="@drawable/shape_back"
    android:gravity="center"
    android:padding="10dp"
    android:text="@string/sensors"
    android:textColor="@color/item_view_color"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@+id/tvElement"
    app:layout_constraintTop_toTopOf="parent" />

<android.support.constraint.Guideline
    android:id="@+id/guideline"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    app:layout_constraintBottom_toTopOf="@+id/rv_channel"
    app:layout_constraintGuide_begin="62dp"
    app:layout_constraintTop_toTopOf="parent" />

<android.support.v7.widget.RecyclerView
    android:id="@+id/rv_channel"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    app:layout_constraintBottom_toTopOf="@+id/btn_ok"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHeight_min="250dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/guideline" />

<android.support.v7.widget.AppCompatButton
    android:id="@+id/btn_ok"
    android:layout_width="wrap_content"
    android:layout_height="25dp"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:background="@drawable/global_button_selector_3"
    android:text="@string/ok"
    android:textColor="@drawable/global_button_txt_selector_3"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/rv_channel" />
</android.support.constraint.ConstraintLayout>
  

结果将是

real device

  

布局检查器显示

layout inspector

  

我有两个问题

     
      
  1. 当此代码在真实设备上运行时,相应的TexView的宽度变为零,并且不再可见。但是,当我删除链时,它正在工作。

  2.   
  3. 它基于RecyclerView,如果它的高度为match_contraints,它将缩小到一个单元格高度,而不管有多少项目,但是当我给出时match_parent,它包含在其中显示的项目中。

  4.   

我不明白我做了什么错误。

感谢您宝贵的时间

2 个答案:

答案 0 :(得分:0)

对于第一个问题,宽度不会变为零,但是您的TextView消失(从屏幕顶部移开),因为它们没有在底部链接,请尝试添加{{1每个TextView

答案 1 :(得分:0)

试试这个。

    <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:background="@color/app_background_color"
    android:minWidth="250dp">

    <TextView
        android:id="@+id/tvElement"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:background="@drawable/shape_back"
        android:gravity="center"
        android:padding="10dp"
        android:text="@string/elements"
        android:textColor="@color/item_view_color"
        app:layout_constraintEnd_toStartOf="@+id/tvSensor"
        app:layout_constraintHorizontal_chainStyle="spread"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/tvSensor"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:background="@drawable/shape_back"
        android:gravity="center"
        android:padding="10dp"
        android:text="@string/sensors"
        android:textColor="@color/item_view_color"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/tvElement"
        app:layout_constraintTop_toTopOf="parent" />

    <android.support.constraint.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintBottom_toTopOf="@+id/rv_channel"
        app:layout_constraintGuide_begin="62dp"
        app:layout_constraintTop_toTopOf="parent" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_channel"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        app:layout_constraintBottom_toTopOf="@+id/btn_ok"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHeight_min="250dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/guideline" />

    <android.support.v7.widget.AppCompatButton
        android:id="@+id/btn_ok"
        android:layout_width="wrap_content"
        android:layout_height="25dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:background="@drawable/global_button_selector_3"
        android:text="@string/ok"
        android:textColor="@drawable/global_button_txt_selector_3"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/rv_channel" />
</android.support.constraint.ConstraintLayout>