Asaf Nevo
Asaf Nevo

Reputation: 11678

Android RelativeLayout alignTop doesn't work

I'm trying to use alignTop attribute in a RelativeLayout in order to set a text to right of a profile picture - aligned to picture's top.

This is my layout:

<?xml version="1.0" encoding="utf-8"?>
<!-- Home fragment layout -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res/com.coapps.pico"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/background_dark_green"
    android:orientation="vertical" >

    <!-- title layout -->

    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/background_fragment_title" >

        <!-- profile picture -->

        <ImageView
            android:id="@+id/fragment_home_profile_picture"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dp"
            android:background="@drawable/background_profile_picture"
            android:contentDescription="@string/app_name"
            android:scaleType="fitXY"
            android:src="@drawable/test_pic" />

        <!-- user name -->

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/fragment_home_profile_picture"
            android:layout_marginLeft="10dp"
            android:layout_toRightOf="@+id/fragment_home_profile_picture"
            android:gravity="top"
            android:text="Roi Mozer"
            android:textColor="@android:color/white"
            android:textSize="30sp"/>

    </RelativeLayout>

</RelativeLayout>

This is how it looks in the preview (and the same in the phone):

As you can see the name center is aligned to picture's top - don't know why.. enter image description here

How can i set them both to be in the same line ?

UPDATE: When i changed the layout height to a given height (and not wrap content) it does work...

Upvotes: 2

Views: 11010

Answers (2)

ramaral
ramaral

Reputation: 6179

The problem may be in your image or background. I tested with another image and both are aligned along the tops.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res/com.coapps.pico"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

android:orientation="vertical" >

<!-- title layout -->

<RelativeLayout
    android:id="@+id/relativeLayout1"
    android:layout_width="wrap_content"
    android:layout_height="200dp"
    android:background="@drawable/background_fragment_title"
    >

    <!-- profile picture -->

    <ImageView
        android:id="@+id/fragment_home_profile_picture"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_centerVertical="true"
        android:layout_marginLeft="10dp"
        android:src="@drawable/falcao_large"
        android:contentDescription="@string/app_name"
        android:scaleType="fitXY"
        />

    <!-- user name -->

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/fragment_home_profile_picture"
        android:layout_marginLeft="10dp"
        android:layout_toRightOf="@+id/fragment_home_profile_picture"
        android:gravity="top"
        android:textColor="@android:color/white"
        android:text="Roi Mozer"

        android:textSize="30sp"/>

</RelativeLayout>

enter image description here

I think problem is image background. The text align to top of background

Upvotes: 2

Hassaan Rabbani
Hassaan Rabbani

Reputation: 2465

Here you go

Add padding to the textView,

Upvotes: 0

Related Questions