Answer:
Adaptation to different resolutions of the android consists of: Values for a LinearLayout
that would be the Pesos(android:layout_weight
).
Explanation:
You can assign the property android:layout_weight
in its XML elements, it would be as if it were a weight(well to say percentage) that is distributed according to the value of this property in all the elements that are relative to its LinearLayout
, would be a sum of all the Weight
'which are declared in Elementos
within your LinearLayout
that in itself has a WeightSum
which is a property that says what is the maximum value(100%
) allowed to their children. By default the value of WeightSum
is 1
.
The property itself of WeightSum
which is the total weight standard of LinearLayout
would be android:weightSum=x
where x
would be the value of Weight (which can be decimal).
The property itself of Layout Weight
which is the value of the weight of a child element of LinearLayout
would be android:layout_weight=x
where x
would be the value of Weight (which can be decimal).
Example:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:weightSum="1" <!-- aqui está o weightSum -->
android:orientation="vertical" >
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/to" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/subject" />
<EditText
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" <!-- aqui está o weight -->
android:gravity="top"
android:hint="@string/message" />
<Button
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="@string/send" />
</LinearLayout>
Upshot:
Remarks:
In the EditText
of the Message you have no values in dp
absolute for height(height), you only have android:layout_weight="1"
that would be the weight of such an element.
If you look at all the child elements of LinearLayout
, will realize that the only element that contains Weight(weight) would be the EditText
of the Message, so the sum of all the Weight’s would be 1
. But if there was another element with 1
of Weight as well, the total Weight would be 2
and would exceed the standard WeightSum
of LinearLayout
that would be 1
.
But if you have one WeightSum
of 10
, for example, you could have 10
child elements containing android:layout_weight=1
each one, so each element would have 10%
full-size.
Very important remark:
You may be wondering: Okay, but where do I let you know if the Weight is for height or width??
The answer is: This is strictly tied to Orientation
of your LinearLayout
that would be this property:
android:orientation="vertical"
From which you may notice that it is stated in LinearLayout
example above, but if he were horizontal Weight’s would be applied horizontally only.
Then, using these properties, the elements could adapt to any screen size?
– Joao Paulo
Yes. They can adapt, but an image for example cannot, because you would have to have different files for each "type" of resolution, okay? there’s another thing.
– Paulo Roberto Rosa
Her answer made me understand what I wanted, but she’s not very objective about the questions that were asked, so I’m not going to mark it as an answer yet. I believe that if you improve, it will be easier to understand to other users interested in the question.
– Joao Paulo
Now it’s better @Joaopaulo ? :)
– Paulo Roberto Rosa
Yes, but I don’t mean to be boring and already being, your answer is more technical, and that’s great. But as I said I do not program for Android yet and was in search of the concept. What I interpreted is that there are elements that can have properties that adapt the resolution of the cell phone and others do not. And that there is no need to make several layouts for different screen sizes.
– Joao Paulo
Exactly, there is no need to make several layouts, only one using
weight
works the same in all resolutions of all cell phones :) but if you do withoutweight
will not work the same on all phones.– Paulo Roberto Rosa