我有以下布局,我想实现的是将middle
ImageView与left
ImageView的promo
对齐(因此,将其居中而不是居中),我知道我可以做到这一点通过删除此约束规则app:layout_constraintStart_toEndOf="@+id/logo"
,问题是middle
ImageView的大小可能会有所不同,因此,如果我删除此约束,并且图像大小太大,它将与logo
ImageView重叠。
<?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">
<ImageView
android:id="@+id/logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/middle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constrainedWidth="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/promo"
app:layout_constraintStart_toEndOf="@+id/logo"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/promo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
对于常规情况应如下所示:
对于这种情况,图像太大应该看起来像这样:
属性app:layout_constraintWidth_default="wrap"
将解决您的问题(宽度设置为0dp
)。中间的ImageView
的大小与使用wrap_content
的大小相同,但是会受到约束的限制(即不会扩展到它们之外)。
检查我针对您情况的解决方案:我想我会将您的中间图像包装在一个布局中(我的exp中的RelativeLayout),设置android:layout_width="0dp"
和