约束视图在ConstraintLayout中移位

时间:2019-03-22 16:47:41

标签: android android-constraintlayout constraint-layout-chains

我在将ConstraintLayout放置在屏幕中央两个视图时遇到了问题。就像标题和内容一样。两者都应垂直居中。所以我的布局看起来像:

<?xml version=“1.0” encoding=“utf-8"?>
<androidx.constraintlayout.widget.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”>

   <TextView
       android:id=“@+id/title”
       android:layout_width=“0dp”
       android:layout_height=“wrap_content”
       android:text=“Title”
       android:textAppearance=“@style/Title1"
       app:layout_constraintBottom_toTopOf=“@id/content”
       app:layout_constraintEnd_toEndOf=“parent”
       app:layout_constraintStart_toStartOf=“parent”
       app:layout_constraintTop_toTopOf=“parent”
       app:layout_constraintVertical_chainStyle=“packed” />

   <EditText
       android:id=“@+id/content”
       android:layout_width=“match_parent”
       android:layout_height=“wrap_content”
       android:inputType=“text|textMultiLine|textNoSuggestions”
       android:textAppearance=“@style/Title4"
       app:layout_constraintBottom_toBottomOf=“parent”
       app:layout_constraintEnd_toEndOf=“parent”
       app:layout_constraintStart_toStartOf=“parent”
       app:layout_constraintTop_toBottomOf=“@id/title”
       tools:text=“body\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\n” />

</androidx.constraintlayout.widget.ConstraintLayout>

但是当内容增长标题移出屏幕边界时。看起来像个错误。如何避免这种行为并使视图位于屏幕边界内? Title view displaced

1 个答案:

答案 0 :(得分:4)

默认情况下,为内容设置了Views的{​​{1}}不会因内容太大而无法满足其约束。要更改此行为并限制尺寸,您需要为wrap_content设置app:layout_constrainedHeight="true"

作为旁注,您还应避免按照documentation中所述,对EditText中包含的match_parent使用Views