Andrew
Andrew

Reputation: 21076

Using layout_above in a RelativeLayout

Can someone explain to me why the ImageView is not appearing above the LinearLayout?

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <LinearLayout
        android:id="@+id/rev_main"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <!-- some stuff in here -->
    </LinearLayout>
    <ImageView
        android:id="@+id/rev_arrow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/arrow"
        android:layout_above="@id/rev_main"
        />
</RelativeLayout>

I don't get it.

Upvotes: 21

Views: 77109

Answers (4)

Oleksii Masnyi
Oleksii Masnyi

Reputation: 2912

If you have such problem in ListView make sure that you use proper inflation method. Parent view group must be specified for correct inflation.

mLayoutInflater.inflate(R.layout.listitem, parent, false);

Don't use

mLayoutInflater.inflate(R.layout.listitem, null);

Upvotes: 0

Opiatefuchs
Opiatefuchs

Reputation: 9870

I had an similar problem by creating a custom optionsMenu. The simplest way to set z-order of views was to do it programmatically. If you want to switch the order sometimes, your should easily call:

    ImageView yourImageView = (ImageView)findViewById(R.id.rev_arrow);
    yourImageView.bringToFront();

I don´t know if it is adaptive to your application, but in my case it works perfect. If you need more code, let me know.

Upvotes: 1

Error 454
Error 454

Reputation: 7315

This happens when you specify alignment relative to another layout. The solution I found was to go the other direction.

Instead of telling the ImageView to be above the LinearLayout, tell the LinearLayout to be below the ImageView.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <LinearLayout
        android:id="@+id/rev_main"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/rev_arrow">
        <!-- some stuff in here -->
    </LinearLayout>
    <ImageView
        android:id="@+id/rev_arrow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/arrow"
        />
</RelativeLayout>

Upvotes: 28

Joseph Earl
Joseph Earl

Reputation: 23432

The following should work for you:

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <ImageView
        android:id="@+id/rev_arrow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/arrow"
        android:layout_alignParentTop="true"
        />
    <LinearLayout
        android:id="@+id/rev_main"
        android:layout_width="fill_parent"
        android:layout_below="@id/rev_arrow"
        android:layout_height="wrap_content">
        <!-- some stuff in here -->
    </LinearLayout>
</RelativeLayout>

Upvotes: 7

Related Questions