YouSub
Untitled

en English

Untitled English subtitles

0 downloads · 167 lines · added Nov 7, 2015

Report a problem
What's wrong?
Advertisement

English transcript

Good job so far.
You're well on your way to building really great layouts.
However, I do see a problem with almost all of the layouts that we built so far.
The views are right up against the edge of the ViewGroup or
up against the device.
For all of the layouts that we've built in this lesson so
far, we could add a little more visual polish to them, so
that they're more beautiful by adding a little bit more white space.
We'll do this using the concepts of padding and margin,
which we'll cover in this video.
For example, we could add a little more white space here, here,
or even at the top, so that the views aren't crowded
up against the edge of the parent and up against the edge of the device.
It's really a design decision though,
because sometimes you do want it to go edge to edge, like for
the full bleed image effect that I was talking about earlier.
But in any case, you can use white space to add a little more breathing room,
so that the content is a little bit easier to read and scan.
I'll show you the effect of adding padding or margin to a view.
Let's start with a TextView that says Hello.
If we add eight dips of padding to the view,
then it makes it bigger by eight dips in every single direction here.
If we add layout margin of eight dips to the TextView,
then this is what it would look like.
In order to add a margin, you need to put it inside a ViewGroup, so
that's why I have it in this linear layout here.
The linear layout is the one that takes original TextView and
pushes it by eight dips.
>From the left and from the top.
And also from the sides as well.
So even though you declare padding or
margin on child TextView, the padding gets handled by the TextView itself.
Whereas the margin gets handled by the parent ViewGroup.
If you both of these on a real device, and you didn't have a background for
the TextView, then technically both would look the same.
the text would start eight dips over from the left, and
eight dips below the top of the device.
However, if you do have a background color for the TextView,
then the user would be able to see that the TextView is this big.
Whereas, on the other device, the TextView would only be this big.
Here's a code for displaying padding on a view.
On the TextView XML Element, you would declare a new attribute that says,
android colon padding equals eight dips in quotation marks.
You can choose whatever size you want for
the padding, as long as it's a dip value.
When we specify eight dips of padding, it adds eight dips to the left,
top, bottom, and right edges of the TextView.
Instead of using the general android padding attribute,
you can also split it out to left, right, top, or bottom.
Essentially, these four lines of code do the same thing as this one line.
However, these fine grained controls allow you to have different padding
values for left or right, or top or bottom.
You don't have to have an equal padding value for all four directions.
The default value for padding is zero, so if you specify left, right, and
top padding, but not bottom, then that will be zero dips of bottom padding.
To specify margin, you add an attribute again to the TextView.
The name of the attribute is android colon layout underscore margin we can
set it equal to, for example, eight dips.
This moves the child TextView eight dips away from the parent on all edges.
If there was another child TextView at the top here, then having a margin would
push this TextView eight dips below that first TextView.
You can think of having a margin as having a little force field around
this view.
No other view can enter this eight dip region around it.
Or you can also split out layout margin into each of the four directions,
layout marginLeft, layout marginRight, top, and bottom.
If you don't specify any of these values, then it will default to zero.
To remember that the ViewGroup is the one that positions these
children based on margin, you can look at the attributes.
They all start with layout underscore for margin.
That way, you know that the ViewGroup is the one who's handling the position
of these views.
Padding and margin can apply to all views.
Let's put the attributes we just learned about into practice.
Here I have three TextViews in a linear layout.
They're all squished up against the left edge of the screen, so
I want to add a little more space.
Let's start by trying to add some margin.
Cool, now there's more space around this TextView.
I'll do the same for the other two TextViews.
Interesting, so this is 16 dips up here, but this is actually 32 dips because
it's getting eight dips of bottom margin from the first TextView and
eight dips of top margin from the second TextView.
If we want the spacing to be consistent, so
it's always 16 dips in between each view then we have to change the margin here.
I can break it out in to individual margin values.
With this code, I have a left margin of 16 dips and a top margin of 16 dips.
There's no bottom margin from this first TextView, so
this space in between is now correct at 16 dips.
The same will go for the last TextView.
Cool, now all the vertical spacing is equal.
Earlier when I said we're adding white spaces to the edges of these views,
technically that space is transparent.
You can see through it, to what's behind it.
In this case we have a gray background on the ViewGroup.
The layout would look the same on the device wether we use margin or
padding, because in this case we have no background color on the TextView.
Let's add it and see where the bounds of each view are.
Okay, now there's a background color on this first TextView.
When we added the left and top margin, we see that the ViewGroup just pushes
the TextView over by eight and down by eight.
We can change this to padding, and now we see that the TextView is larger and
takes up that 8 dips of space on the left and top.
So visually, things would look different on the device,
whether you have padding or margin.
If there is a background color on this TextView.
We can do the same for the other views so you see that it's well.
Adding padding and margin values isn't just restricted to these children views,
you can also add it on the ViewGroup.
If I add a padding value for the linear layout.
Then you can see that it shifts the views over by 16 dips in all directions.
I mentioned that you could set any value for a padding or a margin.
But how do you know which value to pick?
Well, the material design guidelines recommend that you use increments of
eight dips.
This is a little bit more advanced knowledge, but essentially we want our
components to line up on the screen along this eight dip square grid.
Now the squares don't actually show up on the screen, but
if you want to position text here,
you might want to move it over like two squares to the left.
Two squares on this Grid would equal 16 dips.
On this email screen, all the icons are lined up to this line.
They're positioned 16 dips over from the left edge of this screen, and
all this text here is positioned 72 dips over from the left edge of this screen.
So you can kind of imagine that the content is starting to follow certain
lines here.
Here's another way to visualize it.
In this screenshot from the Google I/O app,
we have the content lined up to this 16 dip line, as well as this 72 dip line.
It's essentially 72 dips over from the left edge of the screen.
And on this edge,
the content stops 16 dips before the right edge of the screen.
On the tablet, you have more screen real estate, so
you can have a little more padding.
It can be 24 dips over from the left edge of the screen, and so on for
the other values.
The reason we recommend these values is to make your content easier to read.
For example, if this title was shifted over by a couple pixels, and
the Subtext was shifted over to the right by a couple pixels,
there would be a jagged edge here.
And it would distract the user from actually reading your actual content.
So by lining everything up, it makes your app easier to read and use.
Here are a couple of real life examples of using padding in margin.
In this Google Calendar app screenshot,
we see some text here that's shifted over by a little bit here.
There's also a little bit of space between these calendar events.
If we implement it using padding,
then this background image would probably extend into that blank space.
Instead if we use margin, then it adds a little buffer zone around the view, so
it's probably margin.
Here's another screenshot of the detail page for a calendar event.
There's a lot of text here and there seems to be larger gaps than others.
We can create those larger gaps by adding padding.
To add this gap here, you can either add bottom padding to this TextView, or
you can add top padding to this view.
Either way would work.
Or you could even implement it using layout margin.
You really can't tell the difference between using layout margin or padding
in this case, because the background of the text used is transparent.
Now it's your turn to practice with setting different padding and
margin values.
We've provided some initial code for you to get started using the link below.
Once you're comfortable with padding and margin values, go ahead and
try to build out this layout.
By the way, this text is located 16 dips from the edges here.
But it's ony eight dips in between the two TextViews.
When you're done, check this box.

Need subtitles from another video?