0

In a relativelayout images are cascaded on top of another based on your xml file. How do you indicate what image should be at the very top?

Is there a way to do this in code instead of xml?

I am attaching my XML file for reference:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" 
>

<ImageView
    android:id="@+id/img1"
    android:src="@drawable/payarrow"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
<ImageView
    android:id="@+id/img2"
    android:src="@drawable/chargearrow"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />
<ImageView
    android:id="@+id/landingDockLeft"
    android:src="@drawable/landingdock"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />
<ImageView
    android:id="@+id/landingDockRight"
    android:src="@drawable/landingdock"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />  

roro
  • 127
  • 2
  • 13

3 Answers3

1

In xml, you can use layout_alignParentTop="true" to put the view on the very top. Then make liberal use of layout_below="@+id/id_of_view_to_be_below" to ensure that the view is always below the top view.

In code, you have to use RelativeLayout.LayoutParams with the related attributes and apply them to the child views.

Example in xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" 
>

<ImageView
    android:id="@+id/img1"
    android:src="@drawable/payarrow"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"/>

<ImageView
    android:id="@+id/img2"
    android:src="@drawable/chargearrow"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/img1"
    />
<ImageView
    android:id="@+id/landingDockLeft"
    android:src="@drawable/landingdock"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/img2"
    />
<ImageView
    android:id="@+id/landingDockRight"
    android:src="@drawable/landingdock"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/landingDockLeft"
    />

This will put them all stacked on top of each other with the img1 view at the very top of the entire thing.

The coding way is here.

Community
  • 1
  • 1
DeeV
  • 35,865
  • 9
  • 108
  • 95
  • Hey Deev, thank you but this is not what I wanted. I want the images to be cascaded on top of one another. One layer on top of the other layer. I want them to overlap. However, I want img1 to be directly on top of img3 and img4 on no matter what. Do you know how this can be done? Layout_alignParentTop and stuff only takes care of making sure that the image view exists in a row by itself. Any suggestions? – roro Nov 19 '11 at 06:35
  • The z-order of the views should remain the same throughout the life of the app unless you specify otherwise. You can use attributes such as `layout_alignTop`, `layout_alignBottom`, `layout_alignLeft`, and `layout_alignBottom` to align views to other views based on their locations. I haven't tried this, but I bet if you put `android:layout_alignTop="@+id/img1"` and `android:layout_alignLeft="@id/img1"` to `img2`, it would align img2 to the top left corner of img1. – DeeV Nov 21 '11 at 18:47
0

This is the example: 1) if you put

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" 
>

<ImageView
    android:id="@+id/img1"
    />    
<ImageView
    android:id="@+id/img2"
    />

ImageView with id img1 will be under ImageView with id img2.


2) and if you want to put TextView which will be under ImageView with id img2 you do like so

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" 
    >

    <ImageView
        android:id="@+id/img1"
        />
    <TextView
        android:id="@+id/txt1"
        />        
    <ImageView
        android:id="@+id/img2"
        />

so it is matter of ordering.

AndroidF
  • 125
  • 4
0

android:layout_alignParentTop="true"

Andreas Løve Selvik
  • 1,262
  • 16
  • 25