【问题标题】:How to add one image over other in Android如何在 Android 中将一张图片添加到其他图片之上
【发布时间】:2021-12-23 06:15:44
【问题描述】:

我想创建一个视图,其中一个图像视图(例如 iv_up)应该放在另一个图像视图之上,例如 iv_down

iv_up 应该略微透明。还有一件事,iv_down 恰好是一个二维码。

有什么方法可以实现吗? 以下是我目前编写的代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    android:gravity="center">

    <ImageView
        android:id="@+id/iv_down"
        android:layout_width="419dp"
        android:layout_height="419dp"
        android:layout_centerInParent="true"
        android:contentDescription="QR CODE"
        tools:srcCompat="@tools:sample/avatars"/>

   <ImageView
        android:id="@+id/iv_up"
        android:layout_width="419dp"
        android:layout_height="419dp"
        android:layout_centerInParent="true"
        android:contentDescription="QR CODE"
        tools:srcCompat="@tools:sample/avatars"/>

 

</RelativeLayout>

【问题讨论】:

    标签: android xml


    【解决方案1】:

    看看我创建的这个示例 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"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
        <ImageView
                android:id="@+id/iv_down"
                android:layout_width="200dp"
                android:layout_height="200dp"
                android:layout_centerInParent="true"
                android:contentDescription="QR CODE"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                tools:srcCompat="@tools:sample/avatars" />
    
        <ImageView
                android:id="@+id/iv_up"
                android:layout_width="200dp"
                android:layout_height="200dp"
                android:layout_centerInParent="true"
                android:layout_marginStart="50dp"
                android:layout_marginTop="50dp"
                android:contentDescription="QR CODE"
                app:layout_constraintStart_toStartOf="@id/iv_down"
                app:layout_constraintTop_toTopOf="@id/iv_down"
                tools:srcCompat="@tools:sample/avatars" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    这将创建一个 UI,其中 iv_up 出现在 iv_down 的顶部。使用ConstraintLayout,您可以轻松地相对于彼此定位视图——它是RelativeLayout 的“更好的版本”。所以这里我们将iv_down 设置在视图的左上角,将iv_up 设置为距离iv_down 视图的顶部和开始处50dp。由于视图在 XML 中是这样排序的,因此 iv_up 将始终位于 iv_down 的顶部,因为它的 Z 定位。您可以通过为 android:translationZ 属性设置特定值来更改此设置。

    只需在iv_up 视图上设置android:alpha="0.5" 属性,将使视图透明50%。您可以使用 0-1 的浮点值来获得所需的透明度外观。更改 iv_up 上的边距也会更改视图从左上角移动的距离,即视图重叠的程度!

    【讨论】:

    • 成功了,非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-16
    • 2012-03-09
    • 2015-10-09
    • 1970-01-01
    • 1970-01-01
    • 2020-05-06
    相关资源
    最近更新 更多