2

我在将 2 个视图放置在屏幕中央时遇到了 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”&gt;

   <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>

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

4

1 回答 1

4

默认情况下,Viewswrap_content内容变得太大而无法容纳时,为维度设置的内容不会强制执行其约束。要更改此行为并限制您需要app:layout_constrainedHeight="true"EditText.

作为旁注,您还应该避免使用文档中所述的match_parentfor contains in 。ViewsConstraintLayout

于 2019-03-22T17:19:08.620 回答