android - 如何在 Android 应用程序中对 imageView 进行分层?

标签 android xml android-layout drag-and-drop android-imageview

我正在尝试创建我的第一个真正的 Android 应用程序,代码看起来很简单,但布局给我带来了问题。

我的应用程序将是一个拖放应用程序,非常简单,只需将形状拖到“拼图”中的正确位置即可。这是它现在的样子的一个例子:

enter image description here

我目前有 4 个 ImageView,一个用于顶部的“空拼图”,然后 1 个用于下面的每个形状。我认为做到这一点的正确方法是让拼图中的每个空白点都是一个ImageView(例如,箭头指向的应该是一个 ImageView )

如果我在这一点上是正确的,我需要“分层”ImageView,将 3 个“空形状” ImageView 放在顶部的“拼图” ImageView 上。问题是我在网上的任何地方都找不到这方面的任何示例或建议。所以我的问题是:

  1. 将 3 个 ImageView 置于“背景”ImageView 之上是否有意义,或者有更正确的方法吗?
  2. 如果我的方向正确,有人可以解释/举例说明如何构建 ImageView 层吗?

我当前屏幕的 XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <ImageView
        android:id="@+id/emptyPuz"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/EmptyPuzzle" />
    <LinearLayout 
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:orientation="horizantal" >
       <ImageView
           android:id="@+id/imageView1"
           android:layout_width="0dp"
           android:layout_weight="1"
           android:layout_height="wrap_content"
           android:adjustViewBounds="false"
           android:src="@drawable/Circle" />
        <ImageView
           android:id="@+id/imageView2"
           android:layout_width="0dp"
           android:layout_weight="1"
           android:layout_height="wrap_content"
           android:adjustViewBounds="false"
           android:src="@drawable/Square" />
        <ImageView
           android:id="@+id/imageView3"
           android:layout_width="0dp"
           android:layout_weight="1"
           android:layout_height="wrap_content"
           android:adjustViewBounds="false"
           android:src="@drawable/Triangle" />
   </LinearLayout>
</LinearLayout>

最佳答案

我最终使用的解决方案是 添加一个新的 LinearLayout 并将 background 设置为“puzzle base”图像并添加三个带有“空拼图”的新图像。即:我将 3 个“空”拼图插槽中的每一个分解为单独的图像,如本例所示:

enter image description here

然后将它们用作新布局的背景,所以我之前有这段代码的地方:

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

我现在有:

<LinearLayout
   android:layout_width="fill_parent"
   android:layout_height="0dp"
   android:layout_weight="1"
   android:background="@drawable/emptypuzzle"
   android:orientation="horizontal" >
   <ImageView
       android:id="@+id/dropsquare"
       android:layout_width="0dp"
       android:layout_weight="1"
       android:tag="square"
       android:layout_height="wrap_content"
       android:adjustViewBounds="false"
       android:src="@drawable/emptysquare" />
   <ImageView
       android:id="@+id/droptriangle"
       android:layout_width="0dp"
       android:layout_weight="1"
       android:tag="triangle"
       android:layout_height="wrap_content"
       android:adjustViewBounds="false"
       android:src="@drawable/emptytriangle" />
   <ImageView
       android:id="@+id/dropcircle"
       android:layout_width="0dp"
       android:layout_weight="1"
       android:tag="circle"           
       android:layout_height="wrap_content"
       android:adjustViewBounds="false"
       android:src="@drawable/emptycircle" />
</LinearLayout>

以最终的拖放游戏结束,如下所示:

enter image description here

它不漂亮,但我的 child 喜欢它。 :)

关于android - 如何在 Android 应用程序中对 imageView 进行分层?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18834215/

相关文章:

android - mupdf:如何使用 openBuffer 方法打开 pdf 文件?

Android - 使用库项目时的通用样式资源?

xml - 如何绘制由 IAR Embedded Workbench 生成的 xml 调用图?

python - Notepad++ Python 脚本 - 用小数点计算 XML 数字

java - 如何从MySQL数据库获取数据到android

android - ConstraintLayout layout_constraintHorizo​​ntal_bias 似乎不起作用

android - 如何实现两级 DrawerLayout

android - 如何通过处理程序从 IntentService 多次调用 WebView Activity ?

Android appcompact 不显示上下文操作栏中的文本选择复制操作

java - 第一次打开入职屏幕应该是 list 上的.LAUNCHER?