Question:
More than once I saw in the examples when specifying the height of the View element that they indicate a height equal to 0px . Here is a live example, the ViewPager has a height of 0px :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="scrollable" />
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1"
android:background="@android:color/white" />
</LinearLayout>
Please explain what this means. It turns out ViewPager is displayed in the layout as a horizontal invisible stripe. Wouldn't just wrap by content work (wrap_content)? What is the purpose of this method?
Answer:
layout_height
( layout_width
) equal to 0px is indicated, as far as I can remember, only in one case – when the width (height) is calculated using the "weights" – the android:layout_weight
.
This is done in order to:
Firstly, time was not wasted on the useless calculation of the height specified in the parameter ( wrap_content
and match_parent
are rather resource-intensive attributes), since it will then be changed to the calculated one by weight anyway.
Secondly, when calculating, the value of the current widget size, which is different from zero, will affect the result of calculating the "weight" and you will not get exactly what you expect.