英文:
Android ConstraintLayout: View overlap when constraint to the bottom of a horizontal chain
问题
我想要view3直接位于view1和txt1的下方,没有间隙。
txt1的文本值在运行时是动态的。当txt1占据多行时,所有视图都是正确的。但是当txt1只占据一行时,view1和view3会重叠。
我也只想使用ConstraintLayout,不要使用LinearLayout或其他布局在ConstraintLayout内部。而且只想在XML中进行修改,不要在Java/Kotlin代码中修改。
如何修复它?谢谢!!
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:padding="16dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--其他视图-->
<View
android:id="@+id/view1"
android:layout_width="100dp"
android:layout_height="30dp"
android:background="@android:color/black"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@id/txt1"
/>
<TextView
android:id="@+id/txt1"
android:text="lorem"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintStart_toEndOf="@id/view1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@id/view1"
/>
<View
android:id="@+id/view3"
android:background="@android:color/holo_green_light"
android:layout_width="400dp"
android:layout_height="4dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/txt1"
/>
<!--其他视图-->
</androidx.constraintlayout.widget.ConstraintLayout>
英文:
I wanna view3 directly below view1 and txt1 with no gap.
The text value of txt1 is dynamic at runtime. When txt1 takes multiple lines, all views are correct. But when txt1 takes only one line, view1 and view3 overlap.
Also I wanna use only ConstraitLayout, do not use LinearLayout or other layouts inner ConstraintLayout. And only modify in XML not in Java/Kotlin code
How can I fix it? Thank you!!
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:padding="16dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--other views-->
<View
android:id="@+id/view1"
android:layout_width="100dp"
android:layout_height="30dp"
android:background="@android:color/black"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@id/txt1"
/>
<TextView
android:id="@+id/txt1"
android:text="lorem"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintStart_toEndOf="@id/view1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@id/view1"
/>
<View
android:id="@+id/view3"
android:background="@android:color/holo_green_light"
android:layout_width="400dp"
android:layout_height="4dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/txt1"
/>
<!--other views-->
</androidx.constraintlayout.widget.ConstraintLayout>
答案1
得分: 1
以下是代码部分的翻译:
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:padding="16dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--other views-->
<View
android:id="@+id/view1"
android:layout_width="100dp"
android:layout_height="30dp"
android:background="@android:color/black"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@id/txt1"
/>
<TextView
android:id="@+id/txt1"
android:text="lorem"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintStart_toEndOf="@id/view1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@id/view1"
/>
<androidx.constraintlayout.widget.Barrier
android:id="@+id/barrier"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:barrierDirection="bottom"
app:constraint_referenced_ids="view1,txt1" />
<View
android:id="@+id/view3"
android:background="@android:color/holo_green_light"
android:layout_width="400dp"
android:layout_height="4dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/barrier"
/>
<!--other views-->
</androidx.constraintlayout.widget.ConstraintLayout>
以上是代码的翻译部分,其他文本部分不需要翻译。
英文:
Ahhhh .. here is the scenario where barrier comes into the picture
Kindly check more detail about barrier in this link
https://developer.android.com/reference/androidx/constraintlayout/widget/Barrier
and here is the code that you can use
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:padding="16dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--other views-->
<View
android:id="@+id/view1"
android:layout_width="100dp"
android:layout_height="30dp"
android:background="@android:color/black"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@id/txt1"
/>
<TextView
android:id="@+id/txt1"
android:text="lorem"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintStart_toEndOf="@id/view1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@id/view1"
/>
<androidx.constraintlayout.widget.Barrier
             android:id="@+id/barrier"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             app:barrierDirection=“bottom”
             app:constraint_referenced_ids="view1,txt1" />
<View
android:id="@+id/view3"
android:background="@android:color/holo_green_light"
android:layout_width="400dp"
android:layout_height="4dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/barrier"
/>
<!--other views-->
</androidx.constraintlayout.widget.ConstraintLayout>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。




评论