Aligning textviews
I am developing an android app whose relative layout is given below.
Here is an imgur link to how the app shows on screen: http://imgur.com/c4rNJ. I would like 'Text 1' to appear right above 'a lazy fox ..'. Hope that helps clarify what I wish to accomplish.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:layout_width="fill_parent"
android:background="@color/white">
<TextView android:id="@+id/text1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_marginLeft="0dip"
android:textColor="@color/black" android:text="Text 1" android:lines="1"
android:scrollHorizontally="true" android:ellipsize="end" />
<Button android:id="@+id/checkBox" android:layout_height="wrap_content"
android:layout_width="wrap_content" android:drawableRight="@drawable/checkbox_unchecked_30"
android:layout_below="@+id/text1" android:background="@color/white" />
<TextView android:id="@+id/text2" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_toRightOf="@+id/checkBox"
android:layout_alignTop="@+id/checkBox" android:layout_below="@+id/text1"
android:textColor="@color/blac开发者_运维知识库k" android:text="a lazy fox jumped over the dam and said help"
android:lines="1" android:scrollHorizontally="true" android:ellipsize="end" />
<TextView android:id="@+id/text3" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_toRightOf="@+id/checkBox"
android:layout_below="@+id/text2" android:textColor="@color/black"
android:text="Text 3" />
<TextView android:id="@+id/text4" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_below="@+id/text2"
android:layout_toRightOf="@+id/text3" android:textColor="@color/black"
android:text="Text 4" />
<TextView android:id="@+id/text5" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_below="@+id/text2"
android:layout_toRightOf="@+id/text4" android:textColor="@color/black"
android:text="Text 5" />
</RelativeLayout>
I want "Text 1" to appear right above the text that starts off with "a lazy fox...". Right now 'Text 1' appears above the checkbox. I am not sure how to achieve the above. I have tried various things but nothing seems to be working. What should I be doing to achieve the desired layout?
You need to add layout_alightLeft
attribute to your text1
:
<TextView android:id="@+id/text1"
android:layout_alignLeft="@+id/text2"
...
/>
Full layout file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
<TextView android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="0dip"
android:layout_alignLeft="@+id/text2"
android:text="Text 1"
android:lines="1"
android:scrollHorizontally="true"
android:ellipsize="end" />
<Button
android:id="@+id/checkBox"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_below="@+id/text1"
android:text="button" />
<TextView
android:id="@+id/text2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/checkBox"
android:layout_alignTop="@+id/checkBox"
android:layout_below="@+id/text1"
android:text="a lazy fox jumped over the dam and said help"
android:lines="1"
android:scrollHorizontally="true"
android:ellipsize="end" />
<TextView
android:id="@+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/checkBox"
android:layout_below="@+id/text2"
android:text="Text 3" />
<TextView
android:id="@+id/text4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/text2"
android:layout_toRightOf="@+id/text3"
android:text="Text 4" />
<TextView
android:id="@+id/text5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/text2"
android:layout_toRightOf="@+id/text4"
android:text="Text 5" />
</RelativeLayout>
I was targeting Android 1.5 in which forward references do not work in RelativeLayout. Forward references were first implemented in Android 1.6 (See: http://developer.android.com/resources/articles/ui-1.6.html) and hence any of the suggested solutions by others were not working.
If I understand your requirement correctly, you want the "Text1" to show up right above/aligned with "a lazy fox...".
Try changing the definition of the first TextView to this (notice the last attribute):
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="0dip"
android:textColor="@color/black"
android:text="Text 1"
android:lines="1"
android:scrollHorizontally="true"
android:ellipsize="end"
android:layout_alignLeft="@+id/text2" />
EDIT: Here's the full layout file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:background="@android:color/white">
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="0dip"
android:textColor="@android:color/black"
android:text="Text 1"
android:lines="1"
android:scrollHorizontally="true"
android:ellipsize="end"
android:layout_alignLeft="@+id/text2" />
<Button
android:id="@+id/checkBox"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_below="@+id/text1"
android:drawableRight="@drawable/checkbox_unchecked_30"
android:background="@android:color/white" />
<TextView
android:id="@+id/text2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/checkBox"
android:layout_alignTop="@+id/checkBox"
android:layout_below="@+id/text1"
android:textColor="@android:color/black"
android:text="a lazy fox jumped over the dam and said help"
android:lines="1"
android:scrollHorizontally="true"
android:ellipsize="end" />
<TextView
android:id="@+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/checkBox"
android:layout_below="@+id/text2"
android:textColor="@android:color/black"
android:text="Text 3" />
<TextView
android:id="@+id/text4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/text2"
android:layout_toRightOf="@+id/text3"
android:textColor="@android:color/black"
android:text="Text 4" />
<TextView
android:id="@+id/text5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/text2"
android:layout_toRightOf="@+id/text4"
android:textColor="@android:color/black"
android:text="Text 5" />
</RelativeLayout>
I think you should add android: layout_below="@id/editText2"
to text1
精彩评论