Advertisement
English transcript
Okay, great, so now we know about how ViewGroups can contain views.
Each ViewGroup has specific rules on how it will position the children
inside of it.
First, let's look at LinearLayout.
This ViewGroup can arrange children in a vertical column like this.
Notice how we're not limited to just one TextView and one ImageView.
We can put as many of them as we want in here as well as other types of views.
So this is Tommy, and this is his brother Timmy.
And this is Ivy and her twin sister Irene.
Or the LinearLayout can arrange its children views in a horizontal row
like this.
Hey, if you want to say no boys allowed, that's totally fine with me.
This is Ivy, Irene, and Isabella.
Okay, and that's it for a LinearLayout.
Pretty straightforward, right?
You can arrange children in a vertical column or in a horizontal row.
And then we have dad, the RelativeLayout.
The RelativeLayout, again,
has its own rules for how to position its children within it.
For example, it can position children relative to the parent.
So if this is the parent ViewGroup,
we can have a child TextView aligned to the bottom of the parent and
a child ImageView aligned to the top of the parent.
This also works for aligning to the parent's left and
right edge as well as the center.
The other useful capability of RelativeLayout
is that you can position children relative to other children views.
So, if this is the parent ViewGroup,
then you can position an ImageView on the left edge of the parent and
then position a child TextView to the right of that image.
And then, this child TextView is positioned to the right of the ImageView
but also below this first TextView.
So you see how some of these children views are aligned relative to the parent
or relative to each other.
That was a really quick overview of LinearLayout versus RelativeLayout.
You can see how you might pick one over the other depending
on the type of layout that you want for your app.
Now let's look at the XML for how a ViewGroup contains other views.
In this example,
we'll start with a ViewGroup that contains two child TextViews.
This is a little more code than we're used to, but
I want you to make some observations about this code.
As a hint, the parent code is in red, and the child TextView code is in blue.
We've linked that code below so you can take a look at it and
then write down three observations here.
Each ViewGroup has specific rules on how it will position the children
inside of it.
First, let's look at LinearLayout.
This ViewGroup can arrange children in a vertical column like this.
Notice how we're not limited to just one TextView and one ImageView.
We can put as many of them as we want in here as well as other types of views.
So this is Tommy, and this is his brother Timmy.
And this is Ivy and her twin sister Irene.
Or the LinearLayout can arrange its children views in a horizontal row
like this.
Hey, if you want to say no boys allowed, that's totally fine with me.
This is Ivy, Irene, and Isabella.
Okay, and that's it for a LinearLayout.
Pretty straightforward, right?
You can arrange children in a vertical column or in a horizontal row.
And then we have dad, the RelativeLayout.
The RelativeLayout, again,
has its own rules for how to position its children within it.
For example, it can position children relative to the parent.
So if this is the parent ViewGroup,
we can have a child TextView aligned to the bottom of the parent and
a child ImageView aligned to the top of the parent.
This also works for aligning to the parent's left and
right edge as well as the center.
The other useful capability of RelativeLayout
is that you can position children relative to other children views.
So, if this is the parent ViewGroup,
then you can position an ImageView on the left edge of the parent and
then position a child TextView to the right of that image.
And then, this child TextView is positioned to the right of the ImageView
but also below this first TextView.
So you see how some of these children views are aligned relative to the parent
or relative to each other.
That was a really quick overview of LinearLayout versus RelativeLayout.
You can see how you might pick one over the other depending
on the type of layout that you want for your app.
Now let's look at the XML for how a ViewGroup contains other views.
In this example,
we'll start with a ViewGroup that contains two child TextViews.
This is a little more code than we're used to, but
I want you to make some observations about this code.
As a hint, the parent code is in red, and the child TextView code is in blue.
We've linked that code below so you can take a look at it and
then write down three observations here.