【问题标题】:Why small size Image View not overlapping over the large Image View inside constraint Layout?为什么小尺寸图像视图不与约束布局内的大图像视图重叠?
【发布时间】:2021-10-04 09:44:10
【问题描述】:

我正在尝试将小图像视图重叠在大图像视图上。我已经在模拟器和真实设备中测试了这段代码。没有任何效果。 XML代码sn-p:

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

【问题讨论】:

  • 你能分享一下你想要如何重叠的预览吗?
  • @Bhavin 请检查附件中的预览。

标签: android android-layout android-imageview android-constraintlayout


【解决方案1】:

在您的布局中,这是由于大图像中的高度而发生的。例如,您可以看到大图像的当前高度为 1,小图像为 0,因为此大图像将被提升。要解决此问题,您可以从大图像中删除高程属性,或者根据您的要求在小图像中添加高程

在下面我为两个 ImageView 添加了高程。

ma​​in_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>

上面的布局会给你与你的要求相同的结果

【讨论】:

    【解决方案2】:

    就我而言,两者都是重叠的。我稍微减少了代码。切断不必要的属性:

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

    结果:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-13
      • 1970-01-01
      • 2020-05-17
      • 1970-01-01
      相关资源
      最近更新 更多