首先,您不应该要求将RelativeLayout 嵌套在ConstraintLayout 中。您可能还希望您的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。但是使用类似于我的示例的LinearLayouts 来概念化和构建布局可能更容易。