如何在使用ConstraintLayout约束大小的同时将imageView右对齐

问题描述 投票:0回答:2

我有以下布局,我想实现的是将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>

对于常规情况应如下所示:

enter image description here

对于这种情况,图像太大应该看起来像这样:

enter image description here

android android-imageview android-constraintlayout
2个回答
0
投票

属性app:layout_constraintWidth_default="wrap"将解决您的问题(宽度设置为0dp)。中间的ImageView的大小与使用wrap_content的大小相同,但是会受到约束的限制(即不会扩展到它们之外)。


0
投票

检查我针对您情况的解决方案:我想我会将您的中间图像包装在一个布局中(我的exp中的RelativeLayout),设置android:layout_width="0dp"

© www.soinside.com 2019 - 2024. All rights reserved.