It's All Relative
Android Snippets - Relative Layouts
If you want your scrollable lists in your Android Applications to be smooth and responsive, then you need to know how to use a RelativeLayout
.
Using a RelativeLayout
may seem a little daunting and complicated at first - especially when the UI you are attempting to layout has many elements. However there are only really two things you need to remember:
- you cannot reference an element before it has been declared
- work your way through your layout and identify how your UI Elements are
to be presented:
- Relative to the Container
- Relative to other Elements
- Aligned to other Elements
Relative To Container | |
---|---|
These properties layout elements relative to the parent container. | |
android:layout_alignParentBottom |
positions the bottom of the element on the bottom of the container |
android:layout_alignParentLeft |
positions the left of the element on the left side of the container |
android:layout_alignParentRight |
positions the right of the element on the right side of the container |
android:layout_alignParentTop |
positions the element at the top of the container |
android:layout_centerHorizontal |
centers the element horizontally within its parent container |
android:layout_centerInParent |
centers the element both horizontally and vertically within its container |
android:layout_centerVertical |
centers the element vertically within its parent container |
Relative To Other Elements | |
These properties allow you to layout elements relative to other elements on screen. The value for each of these elements must defined using android:id=”@+id/myelementlabel”, and as mentioned earlier, you can not reference an element before it has been declared. | |
android:layout_above |
positions the element above the specified element |
android:layout_below |
positions the element below the specified element |
android:layout_toLeftOf |
positions the element to the left of the specified element |
android:layout_toRightOf |
positions the element to the right of the specified element |
Aligned to other Elements | |
These properties allow you to specify how elements are aligned in relation to other elements. | |
android:layout_alignBaseline |
aligns the baseline of your element with the baseline of the specified element. |
android:layout_alignBottom |
aligns the bottom of your element in with the bottom of the specified element |
android:layout_alignLeft |
aligns left edge of your element with the left edge of the specified element |
android:layout_alignRight |
aligns right edge of your element with the right edge of the specified element |
android:layout_alignTop |
positions top of the your element in alignment with the top of the specified element |