如何解决为什么小尺寸的图像视图不会与约束布局内的大图像视图重叠?
<?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"
android:id="@+id/itemCard"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- large ImageView -->
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/imageViewPhoto"
android:layout_width="match_parent"
android:layout_height="@dimen/_145sdp"
android:background="@color/grey_lighter"
android:elevation="@dimen/_1sdp"
android:scaleType="centerCrop"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay.App.CornerSize50Percent"
app:srcCompat="@mipmap/dummy" />
<!-- small ImageView -->
<androidx.appcompat.widget.AppCompatImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/typ_rounded_view"
android:paddingStart="@dimen/_8sdp"
android:paddingTop="@dimen/_3sdp"
android:paddingEnd="@dimen/_4sdp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:srcCompat="@drawable/ic_video" />
</androidx.constraintlayout.widget.ConstraintLayout>
约束布局梯度:
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
Android Studio 版本:4.2.2
解决方法
在您的布局中,这是由于大图像中的高程而发生的。例如,您可以看到大图像的当前高度为 1,小图像的当前高度为 0,因为此大图像将被升高。要解决此问题,您可以从大图像中删除高程属性,也可以根据您的要求在小图像中添加高程
接下来,我为两个 ImageView 添加了海拔。
main_layout.xml
<?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"
android:id="@+id/itemCard"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- large ImageView -->
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/imageViewPhoto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/grey_lighter"
android:elevation="1dp"
android:scaleType="centerCrop"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay.App.CornerSize50Percent"
app:srcCompat="@mipmap/ic_launcher" />
<!-- small ImageView -->
<androidx.appcompat.widget.AppCompatImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/typ_round_view"
android:elevation="1dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:srcCompat="@drawable/ic_camera" />
</androidx.constraintlayout.widget.ConstraintLayout>
typ_round_view.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/grey_darker" />
<corners android:topLeftRadius="30dp" />
<padding
android:bottom="0dp"
android:left="18dp"
android:right="8dp"
android:top="8dp" />
</shape>
上面的布局会给你与你的要求相同的结果
,就我而言,两者都是重叠的。我减少了一些代码。切断不必要的属性:
<?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"
android:id="@+id/itemCard"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- large ImageView -->
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/imageViewPhoto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/my_image" />
<!-- small ImageView -->
<androidx.appcompat.widget.AppCompatImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:srcCompat="@drawable/my_image2" />
</androidx.constraintlayout.widget.ConstraintLayout>
结果:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。