如何在约束布局中设置visibilty后分配空白空间

时间:2018-03-19 12:28:02

标签: android android-constraintlayout

我试图为Recyclerview绘制一个示例单元格,其中包含复选框,标题和详细信息以及图像

我的问题是在尝试设置细节的可见性时,标题仍然没有从详细信息视图中分配空间,所以我怎么能完成这个?

enter image description here

和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"
    android:layout_width="match_parent"
    android:layout_height="@dimen/_55sdp"
    xmlns:tools="http://schemas.android.com/tools">


    <RadioButton
        android:id="@+id/rBToggle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/_10sdp"
        android:checked="false"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


    <ImageView
        android:id="@+id/arrowDown"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="@dimen/_10sdp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_arrow_down" />

    <TextView
        android:id="@+id/tvFreqTitle"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/_10sdp"
        android:layout_marginTop="@dimen/_10sdp"
        tools:text="Title "
        android:textColor="@color/black"
        android:textSize="@dimen/_15ssp"
        app:layout_constraintEnd_toStartOf="@+id/arrowDown"
        app:layout_constraintStart_toEndOf="@+id/rBToggle"
        app:layout_constraintTop_toTopOf="parent" />


    <TextView
        android:id="@+id/tvFreqDetails"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/_10sdp"
        tools:text="Details "
        android:textColor="#8a898e"
        android:textSize="@dimen/_10ssp"
        android:visibility="visible"
        app:layout_constraintEnd_toStartOf="@+id/arrowDown"
        app:layout_constraintStart_toEndOf="@+id/rBToggle"
        app:layout_constraintTop_toBottomOf="@+id/tvFreqTitle"
        app:layout_goneMarginTop="0dp" />


    <View
        android:id="@+id/separator"
        android:layout_width="0dp"
        android:layout_height="0.5dp"
        android:background="#c8c7cc"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />


</android.support.constraint.ConstraintLayout>

3 个答案:

答案 0 :(得分:-1)

你必须在constraintLayout里面添加一个linearLayout,在这个Linear中你放两个textView

有一个sourceCode

    <?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="55dp"
    xmlns:tools="http://schemas.android.com/tools">


    <RadioButton
        android:id="@+id/rBToggle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="10dp"

        android:checked="false"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


    <ImageView
        android:id="@+id/arrowDown"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="10dp"
        android:layout_marginStart="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_arrow_down" />


    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="0dp"
        android:layout_height="wrap_content"

        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/arrowDown"
        app:layout_constraintStart_toEndOf="@+id/rBToggle"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_goneMarginTop="0dp">


        <TextView
            android:id="@+id/tvFreqTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textColor="#000000"
            android:textSize="15sp"

            tools:text="Title " />

        <TextView
            android:id="@+id/tvFreqDetails"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textColor="#8a898e"
            android:textSize="10sp"
            android:visibility="visible"

            tools:text="Details " />
    </LinearLayout>


    <View
        android:id="@+id/separator"
        android:layout_width="0dp"
        android:layout_height="0.5dp"
        android:background="#c8c7cc"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />


</android.support.constraint.ConstraintLayout>

我改变了尺寸因为我没有dimen文件所以你必须更新它:) 希望这很有用:)

更新布局

<?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="55dp"
    xmlns:tools="http://schemas.android.com/tools">


    <RadioButton
        android:id="@+id/rBToggle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="10dp"

        android:checked="false"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


    <ImageView
        android:id="@+id/arrowDown"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="10dp"
        android:layout_marginStart="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_arrow_down"  />


    <TextView
        android:id="@+id/tvFreqDetails"
        android:layout_width="0dp"
        android:layout_height="11dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"

        android:layout_weight="1"
        android:textColor="#8a898e"
        android:textSize="10sp"
        android:visibility="visible"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/arrowDown"
        app:layout_constraintStart_toEndOf="@+id/rBToggle"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0"
        tools:text="Details " />

    <TextView
        android:id="@+id/tvFreqTitle"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"

        android:layout_marginTop="8dp"
        android:layout_weight="1"
        android:textColor="#000000"
        android:textSize="15sp"
        app:layout_constraintBottom_toTopOf="@+id/tvFreqDetails"
        app:layout_constraintEnd_toStartOf="@+id/arrowDown"
        app:layout_constraintStart_toEndOf="@+id/rBToggle"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="Title " />


    <View
        android:id="@+id/separator"
        android:layout_width="0dp"
        android:layout_height="0.5dp"
        android:background="#c8c7cc"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />


</android.support.constraint.ConstraintLayout>

答案 1 :(得分:-1)

您可以使用setVisibility(View.INVISIBLE)这将隐藏标题,但其空间将被保留。

答案 2 :(得分:-1)

<?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"
    android:layout_width="match_parent"
    android:layout_height="55dp"
    android:orientation="horizontal"
    xmlns:tools="http://schemas.android.com/tools">


    <RadioButton
        android:id="@+id/rBToggle"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:layout_marginStart="10dp"
        android:checked="false" />

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:layout_gravity="center_vertical"
    android:orientation="vertical">


    <TextView
        android:id="@+id/tvFreqTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:layout_marginTop="10dp"
        tools:text="Title "
        android:gravity="center_vertical"
        android:textColor="@color/colorPrimaryDark"
        android:textSize="15sp" />


    <TextView
        android:id="@+id/tvFreqDetails"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        tools:text="Details "
        android:textColor="#8a898e"
        android:textSize="10sp"
        android:visibility="gone"
        />

</LinearLayout>

    <ImageView
        android:id="@+id/arrowDown"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="10dp"
        app:srcCompat="@drawable/common_google_signin_btn_icon_dark" />


    <View
        android:id="@+id/separator"
        android:layout_width="0dp"
        android:layout_height="0.5dp"
        android:background="#c8c7cc" />


</LinearLayout>