实际上,这是ConstraintLayout常见的问题,基本上有两个选项。一种是如果您的ImageView高度固定,则将TextView最小高度设置为ImageView的高度;另一种是现在引入了Barrier,但这是Beta版本,如果要在Beta版本中使用,请按以下设置进行设置。
这是ConstraintLayout中引入的新功能,目前处于Beta版本。
如何将beta ConstraintLayout添加到项目中,请按照以下步骤进行设置。
在项目gradle文件中添加maven支持,如下所示。
allprojects {
repositories {
maven { url 'https://maven.google.com' }
jcenter()
}
}
然后在应用程序的gradle依赖项中添加ConstraintLayout库依赖
compile 'com.android.support.constraint:constraint-layout:1.1.0-beta3'
在布局中添加障碍。
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@mipmap/ic_launcher" />
<TextView
android:id="@+id/textView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/imageView2"
app:layout_constraintTop_toTopOf="parent" />
<android.support.constraint.Barrier
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/barrier1"
app:barrierDirection="bottom"
app:constraint_referenced_ids="textView2, imageView2"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="TextView"
app:layout_constraintTop_toBottomOf="@id/barrier1"
app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>
Beta3现已发布,修复了一些问题并增加了新功能
https://androidstudio.googleblog.com/2017/10/constraintlayout-110-beta-3-is-now.html
ProgressBar
可以是任何类型的视图。非常感谢您提供的出色建议。 - Jiaqi Liu