0

enter image description here

Why is there space between the top of the text and the top of the image??

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout

    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_margin="9dp"
    android:background="#FFF">
    <RelativeLayout

        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="9dp">
        <ImageView
            android:id="@+id/img_user"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_marginRight="9dp"
            android:background="#000" />

        <TextView
            android:id="@+id/username"
            android:layout_toRightOf="@id/img_user"
            android:layout_alignTop="@id/img_user"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#333"
            android:textStyle="bold"
            android:textSize="12dp"
            android:gravity="top"
            android:text="Username" />
        <TextView
            android:id="@+id/timestamp"
            android:layout_toRightOf="@id/img_user"
            android:layout_alignBottom="@id/img_user"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#999"
            android:textSize="10dp"
            android:gravity="bottom" 
            android:text="2h"/>
    </RelativeLayout>
hunterp
  • 15,716
  • 18
  • 63
  • 115
  • what is the gravity for in the textView?, you have set it to wrap_content ,so gravity will not take any effect, can you check without it? – Yashwanth Kumar Sep 23 '11 at 04:23
  • 1
    I think that is the way TextViews work. It takes into account the symbol with the highest possible pixel, and the symbol with the lowest possible pixel and that is how it determines its top and bottom. I think if you had something like a "p" in the bottom TextView the very bottom of the line in the "p" would be touching the baseline of the image. I am not positive though. – FoamyGuy Sep 23 '11 at 13:10

2 Answers2

0

This is an inherent part of TextView. Try using margins, and consider wrapping extra layouts around to give you room to move things.

hunterp
  • 15,716
  • 18
  • 63
  • 115
  • For the bottom, also consider android:layout_alignBaseline, which will align the baseline of the text to the bottom of the other view, instead of the bottom of the text view to the bottom of the other view. For the top.. yeah. Play with Margins. – Mooing Duck Mar 09 '17 at 01:44
-1

You have set a margin field in the linear layout and then again in the relative layout.

android:layout_margin="9dp"
Anju
  • 9,379
  • 14
  • 55
  • 94