Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialMichael Partridge
7,129 PointsInstead of using gravity why not use layout_gravity?
I get the same results when I leave contents as wrapped and change layout_gravity
to center (both)
. Is there a particular reason you chose to match_parent
and use the gravity
attribute? Any known bugs with my method or is it just a matter of personal preference?
1 Answer
Daniel Hartin
18,106 PointsHi Michael
Gravity aligns the content inside the layout where layout_gravity aligns how the layout should fit inside it's parent. It definitely matters which ones you use, while it may look the same on your device or the emulator it may look completely different on say a 10" tablet.
A good explanation can be found here
http://stackoverflow.com/questions/3482742/gravity-and-layout-gravity-on-android
Hope this helps Daniel
Michael Partridge
7,129 PointsMichael Partridge
7,129 PointsUnderstood, but if you have that view set to
wrap_content
isn'tlayout_gravity
going to position the view the same way it would if I had instead usedmatch_parent
andgravity
?Daniel Hartin
18,106 PointsDaniel Hartin
18,106 PointsIt may do for this situation but let's say your text in the text view is longer than one line or it is an imageview with a smaller image than the width of the screen. The text or image would be centered in the view but the view would default to align start leaving a potential large gap on the right. Like I say for this exercise it may well be fine but it is better to set your view planning for different data as it may change in the future and it's less then to worry about
Daniel
Michael Partridge
7,129 PointsMichael Partridge
7,129 PointsOkay thank you!