0

Text failing to align

The "sun is red" should align to the top of the image, and "2pm" should align to the bottom. Why don't attributes layout_alignTop and layout_alignBottom work as documented?

  <RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="9dp">
        <ui.RoundedImageView
        android:id="@+id/img_user"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="9dp"
        st:cornerRadius="3"
        android:background="@drawable/black_rounded3" />
        <TextView
                android:id="@+id/uname"
                android:layout_toRightOf="@id/img_user"
                android:layout_alignTop="@id/img_user"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#000000"
                android:textStyle="bold" />
        <TextView
                android:id="@+id/time"
                android:layout_toRightOf="@id/img_user"
                android:layout_alignBottom="@id/img_user"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#909191" />

        <LinearLayout
                android:id="@+id/layout_g"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="9dp"
                android:layout_marginLeft="9dp"
                android:layout_alignParentRight="true"
                android:clickable="true"
                android:gravity="right">
                <TextView
                        android:gravity="right"
                        android:layout_gravity="right"
                        android:id="@+id/gtext"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        style="@style/text_detail" />
        </LinearLayout>
      </RelativeLayout>
hunterp
  • 15,716
  • 18
  • 63
  • 115
  • you have a lot of textviews in the layout, can you tell us which one corresponds to "the sun is red" and which one corresponds to "2pm" – slayton Sep 22 '11 at 21:28

2 Answers2

4

Why don't attributes layout_alignTop and layout_alignBottom work as documented?

-> They do.

The problem is with the RoundedImageView object you're using. My guess is that it is onMeasure is not correct and is not returning proper value for where the top and bottom of itself is. With out knowing anything else about this RoundedImageView that you're using I can't really point you in a direction to get it fixed.

I changed your code to this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="9dp">
    <ImageView
    android:id="@+id/img_user"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="9dp"

    android:background="@drawable/black" />
    <TextView
            android:id="@+id/uname"
            android:layout_toRightOf="@id/img_user"
            android:layout_alignTop="@id/img_user"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#000000"
            android:text="the sun is red"
            android:textStyle="bold" />
    <TextView
            android:id="@+id/time"
            android:layout_toRightOf="@id/img_user"
            android:text="2pm"
            android:layout_alignBottom="@id/img_user"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#909191" />

    <LinearLayout
            android:id="@+id/layout_g"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="9dp"
            android:layout_marginLeft="9dp"
            android:layout_alignParentRight="true"
            android:clickable="true"
            android:gravity="right">
            <TextView
                    android:gravity="right"
                    android:layout_gravity="right"
                    android:id="@+id/gtext"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    />
    </LinearLayout>
  </RelativeLayout>

And it properly aligns the text. Here is screenshot:

enter image description here

FoamyGuy
  • 46,603
  • 18
  • 125
  • 156
  • Thanks, insightful, will examine now. – hunterp Sep 23 '11 at 03:05
  • Still why is there space between the top and bottom?? I've asked in another question: http://stackoverflow.com/questions/7524166/why-is-there-a-gap-when-i-aligntop – hunterp Sep 23 '11 at 04:18
0

I had a similar problem with a customized view (in my case directly inherited from android.view.View) and a RelativeLayout.

I did not overwrite the onMeasure() method and used the canvas that I got in onDraw() as is to write my graphics into it. The graphical output was completely ok, but the alignment of other views in the RelativeLayout did not work as in the case described here.

It finally turned out that my constructor did not call the constructor of the superclass properly (I forgot to pass the AttributeSet). After I changed the first line of my constructor to

public MyNewView (Context context, AttributeSet attrs) {
    super(context, attrs);
    ...
}

all of a sudden everything worked well.

Seems as if some magic of the layout algorithm of Android is already happening in the constructor of android.View.view long before onMeasure() is called.

Nantoka
  • 4,174
  • 1
  • 33
  • 36