如何将 ImageView 放置在另一个 ImageView 之上?

How to place ImageViews on top of another ImageView?

我有一个非常简单的问题。我想将 ImageViews 放在这张吉他指板图片的顶部,这样我就可以制作一个简单的指法谱程序,这与 songsterr 不同,但非常简单。

这是当前视图的样子。

Guitar Fretboard

这是代码

<?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"
tools:context=".ui.home.HomeFragment">

<RelativeLayout
    android:orientation="vertical"
    android:gravity="top"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:ignore="MissingConstraints">

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <HorizontalScrollView
            android:id="@+id/horizontalScrollView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >


            <LinearLayout
                android:gravity="top"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" >

                <ImageView
                    android:id="@+id/fretboardView"
                    android:layout_width="fill_parent"
                    android:layout_height="777dp"
                    android:layout_gravity="center"
                    android:src="@drawable/fretboard" />


            </LinearLayout>
        </HorizontalScrollView>
    </ScrollView>

</RelativeLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

这是我希望它看起来像的示例。

With frets

我试图做的是使我的根布局成为相对布局,并将它们放在我想要的位置,但是当我将另一个 ImageView 拖到我的视图中时,它只是将它放在屏幕之外。感谢那些花时间回答的人。

首先,您不应该要求在 ConstraintLayout 中嵌套 RelativeLayout。您还可能希望将 ScrollView 作为顶层布局,并将根布局置于 ScrollView 内。此外,您将 HorizontalScrollView 嵌套在 ScrollView 中的目的是什么?您是否需要垂直和水平滚动的视图?

ConstraintLayout 中,您可以相对于彼此或父 ConstraintLayout 视图布局任何视图。这包括在其他子视图之上放置一些子视图。

另一种选择是使用 FrameLayout,它包裹背景 ImageView 的内容。然后使用 LinearLayout 将较小的视图放在顶部。例如:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/fretboardView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/fretboard" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/dot1"
            android:layout_width="wrap_content"
            android:layout_weight="wrap_content"
            android:src="@drawable/dot" />

        <ImageView
            android:id="@+id/dot2"
            android:layout_width="wrap_content"
            android:layout_weight="wrap_content"
            android:src="@drawable/dot" />

    </LinearLayout>

</FrameLayout>

您可能还需要使用另一个垂直方向 LinearLayout 来沿着品格中的琴弦布置点。例如:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/fretboardView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/fretboard" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="vertical">


            <ImageView
                android:id="@+id/dot1"
                android:layout_width="wrap_content"
                android:layout_weight="wrap_content"
                android:src="@drawable/dot" />

            <ImageView
                android:id="@+id/dot2"
                android:layout_width="wrap_content"
                android:layout_weight="wrap_content"
                android:src="@drawable/dot" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="vertical">


            <ImageView
                android:id="@+id/dot1"
                android:layout_width="wrap_content"
                android:layout_weight="wrap_content"
                android:src="@drawable/dot" />

            <ImageView
                android:id="@+id/dot2"
                android:layout_width="wrap_content"
                android:layout_weight="wrap_content"
                android:src="@drawable/dot" />

        </LinearLayout>

    </LinearLayout>

</FrameLayout>

您还需要在不需要点的地方添加空格。例如:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/fretboardView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/fretboard" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="vertical">


            <ImageView
                android:id="@+id/dot1"
                android:layout_width="wrap_content"
                android:layout_weight="wrap_content"
                android:src="@drawable/dot" />

            <Space
                android:layout_width="match_parent"
                android:layout_height="@dimen/dot_size" />

            <ImageView
                android:id="@+id/dot2"
                android:layout_width="wrap_content"
                android:layout_weight="wrap_content"
                android:src="@drawable/dot" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="vertical">


            <ImageView
                android:id="@+id/dot1"
                android:layout_width="wrap_content"
                android:layout_weight="wrap_content"
                android:src="@drawable/dot" />

            <Space
                android:layout_width="match_parent"
                android:layout_height="@dimen/dot_size" />

            <Space
                android:layout_width="match_parent"
                android:layout_height="@dimen/dot_size" />

            <ImageView
                android:id="@+id/dot2"
                android:layout_width="wrap_content"
                android:layout_weight="wrap_content"
                android:src="@drawable/dot" />

        </LinearLayout>

    </LinearLayout>

</FrameLayout>

其中 @dimen/dot_size 定义为其中一个点的大小。

您可以使用不同布局类型的多种变体。 ConstraintLayout 的优点是只需要一个根 ViewGroup。但是使用类似于我的示例的 LinearLayout 来概念化和构建布局可能更容易。