Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android RelativeLayout spacing

Tags:

android

I'm just putting the finishing touches to my Android app. Unfortunately, I dug straight into development without reading the documentation and built my layout with AbsoluteLayout and it turned out to look terrible when I loaded the app on my phone. Now I'm redoing the UI in a RelativeLayout and I want to put empty canvas space in between my ViewGroups in the y-direction. I am currently achieving this by putting random TextView sentences that are of the same color as my View's background in order to make psuedo-empty space. Is there a better way to do this, because right now when I define a specific ViewGroup to be placed below another View, it gets stuck right below the top View.

As I was writing this, it dawned upon me that using padding might be the answer.... Any other suggestions?

like image 854
Sachin Avatar asked Jun 13 '10 17:06

Sachin


People also ask

How android RelativeLayout works?

RelativeLayout is a view group that displays child views in relative positions. The position of each view can be specified as relative to sibling elements (such as to the left-of or below another view) or in positions relative to the parent RelativeLayout area (such as aligned to the bottom, left or center).

Which is better constraint layout or RelativeLayout?

ConstraintLayout has flat view hierarchy unlike other layouts, so does a better performance than relative layout. Yes, this is the biggest advantage of Constraint Layout, the only single layout can handle your UI.

How does Relative layout work?

Android RelativeLayout enables you to specify how child views are positioned relative to each other. The position of each view can be specified as relative to sibling elements or relative to the parent.


1 Answers

For each view in your xml layout, you can apply the android:layout_margin* where * is Top, Bottom, Left, or Right. You will also want to make sure to use dip units so it spaces the same on different displays.

One thought, you might want to be careful of using margins and padding to get it to look like you want, because phones like the droid with a taller screen than "standard" can really mess things up.

I suggest looking into placing frame style elements in their position using layout_alignParent* (Top, Bottom, etc.) and then, if, for example, you have a box of open space on the screen, align its corners with the framework elements which will be located correctly because of the parent alignment, and then use a sub LinearLayout that is centered (android:gravity="center_horizontal") for the interface buttons or whatever.

like image 150
Guzba Avatar answered Oct 05 '22 08:10

Guzba