所以我有一个像这样的约束
<ConstraintLayout>
<TextView
android:id="@+id/text_view"
app:layout_constraintEnd_toStartOf="@+id/view1" />
<View
android:id="@+id/view1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<View
android:id="@+id/view2"
aapp:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</ConstraintLayout>
目前textview的结尾位于view1的开头,但是当view1的可见性消失时,我希望textview的结尾到view2的开头,否则长字符串可能会覆盖view2。有没有办法将约束设置为两个视图,还是有更好的解决方案?
答案 0 :(得分:0)
您可以将GlobalLayoutListener设置为您的视图,以处理this answer中所述的可见性更改,然后更改与其可见性状态相关的约束:
ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone(constraintLayout);
constraintSet.connect(R.id.text_view,ConstraintSet.RIGHT,R.id.view2,ConstraintSet.LEFT,0);
constraintSet.applyTo(constraintLayout);
答案 1 :(得分:0)
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:layout_editor_absoluteY="25dp">
<android.support.v7.widget.AppCompatTextView
android:id="@+id/autoCompleteTextView"
android:layout_width="0dp"
android:layout_height="84dp"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:text="HELLO WORLD"
android:textSize="18dp"
android:gravity="center_horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="@+id/view2"
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_marginTop="8dp"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="@+id/autoCompleteTextView"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="@+id/autoCompleteTextView"
app:layout_constraintTop_toBottomOf="@+id/autoCompleteTextView" />
<View
android:id="@+id/view1"
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_marginBottom="16dp"
android:layout_marginTop="8dp"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/autoCompleteTextView"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="@+id/autoCompleteTextView"
app:layout_constraintTop_toBottomOf="@+id/view2" />
</android.support.constraint.ConstraintLayout>
试试这个...希望它可以帮助你......
答案 2 :(得分:0)
最后,我发现我可以将view1的可见性设置为INVISIBLE而不是GONE以使其工作,因为它仍占用空间,textview不会扩展。
答案 3 :(得分:0)
您可以使用屏障来约束多个视图。
<ConstraintLayout>
<androidx.constraintlayout.widget.Barrier
android:id="@+id/barrier"
app:barrierDirection="left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:constraint_referenced_ids="view1,view2"/>
<TextView
android:id="@+id/text_view"
app:layout_constraintEnd_toStartOf="@id/barrier" />
<View
android:id="@+id/view1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<View
android:id="@+id/view2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</ConstraintLayout>
文本视图将被限制在更左侧的视图中