I have a xml layout to enter a pincode:
<?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:background="#FF000000"
android:orientation="vertical"
android:weightSum="1" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_margin="5dp"
android:layout_weight="0.2"
android:background="@android:drawable/edit_text"
android:gravity="center_vertical"
android:orientation="horizontal" >
<EditText
android:id="@+id/pinDisplay"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="0.85"
android:background="@null" />
<ImageButton
android:id="@+id/backspace"
style="@android:style/Widget.Button"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="center_vertical"
android:layout_margin="3dp"
android:layout_weight="0.15"
android:gravity="center"
android:src="@android:drawable/ic_input_delete" />
</LinearLayout>
<!-- Keypad section -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="0.8"
android:baselineAligned="false"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_marginLeft="5dip"
android:layout_marginRight="4dip"
android:layout_weight="1"
android:orientation="horizontal" >
<Button
android:id="@+id/one"
android:layout_width="0sp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="1"
android:textAppearance="@android:attr/text" />
<Button
android:id="@+id/two"
android:layout_width="0sp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="2" />
<Button
android:id="@+id/three"
android:layout_width="0sp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="3" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0sp"
android:layout_marginLeft="5dip"
android:layout_marginRight="4dip"
android:layout_weight="1"
android:orientation="horizontal" >
<Button
android:id="@+id/four"
android:layout_width="0sp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="4" />
<Button
android:id="@+id/five"
android:layout_width="0sp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="5" />
<Button
android:id="@+id/six"
android:layout_width="0sp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="6" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0sp"
android:layout_marginLeft="5dip"
android:layout_marginRight="4dip"
android:layout_weight="1"
android:orientation="horizontal" >
<Button
android:id="@+id/seven"
android:layout_width="0sp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="7" />
<Button
android:id="@+id/eight"
android:layout_width="0sp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="8" />
<Button
android:id="@+id/nine"
android:layout_width="0sp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="9" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0sp"
android:layout_marginBottom="10dip"
android:layout_marginLeft="5dip"
android:layout_marginRight="4dip"
android:layout_weight="1"
android:orientation="horizontal" >
<Button
android:id="@+id/ok"
android:layout_width="0sp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:enabled="false"
android:text="OK"
android:textColor="#B8B8B8" />
<Button
android:id="@+id/zero"
android:layout_width="0sp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="0" />
<Button
android:id="@+id/clear"
android:layout_width="0sp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="Clear" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
The layout is fine, but I get a lot of Lint warnings about nested weights.
Nested weights are bad for performance
I don't really notice the layout loading slow or anything. What can I do to avoid nested weights? Or should I just leave it like it is?
Nested weights are bad for performance because: Layout weights require a widget to be measured twice. When a LinearLayout with non-zero weights is nested inside another LinearLayout with non-zero weights, then the number of measurements increase exponentially.
RelativeLayout lets child views specify their position relative to the parent view or to each other (specified by ID). So you can align two elements by right border, or make one below another, centered in the screen, centered left, and so on.
In general, you should try to use a RelativeLayout instead of nesting many LinearLayouts.
From documentation:
Note: Although you can nest one or more layouts within another layout to acheive your UI design, you should strive to keep your layout hierarchy as shallow as possible. Your layout draws faster if it has fewer nested layouts (a wide view hierarchy is better than a deep view hierarchy).
But when you need weights, like now, a RelativeLayout wouldn't help you much; instead, I suggest using a TableLayout for your keypad, to reduce the number of nested layouts.
You could use RelativeLayout to avoid the nested weights.
Nested weights are bad for performance because the number of measurements increase exponentially with each one nested.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With