How do you layer imageViews in an android applicat

2019-06-05 03:23发布

I'm trying to create my first real Android app, the code seems easy enough, but the layout is giving me problems.

My app will be a drag and drop application, very simple, just drag shapes to the correct place in the "puzzle". Here's an example of how it looks right now:

enter image description here

What I have currently is 4 ImageViews, one for the "empty puzzle" at the top, then 1 for each of the shapes below. I think the correct way to do this is to have each of the empty spots in the puzzle be an ImageView (what the arrow is pointing to, for example, should be an ImageView)

If I am correct on this, I need to "layer" ImageViews, put 3 "empty shape" image views over the "puzzle" image view at the top. The problem is I can't find any examples or suggestions for this anywhere online. So my questions are:

  1. Does it make sense to have 3 ImageViews sitting on top of a "background" ImageView, or is there a more correct way to do this?
  2. If I'm heading in the right direction, could someone explain/examplify how one builds ImageView layers?

XML for my current screen:

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

1条回答
时光不老,我们不散
2楼-- · 2019-06-05 04:02

The solution I ended up using was to add a new LinearLayout with the background set to a "puzzle base" image and added three new images with "empty puzzle" pieces on it. Ie: I broke up each of the 3 "empty" puzzle slots into individual images like in this example:

enter image description here

Then used them as backgrounds to a new layout, so where I had this code before:

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

I now have:

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

Ending with a resultant drag and drop game looking like:

enter image description here

It's not pretty, but my toddler likes it. :)

查看更多
登录 后发表回答