Is working with 'Weights' viable?

Asked

Viewed 130 times

1

I’m mounting a layout here and after include the property: android:layout_weight, the eclipse returns a warning: 'nested Weights bad for performance'.

Why work with this property, is it bad for performance? And to what extent, it will influence performance/performance?

Thank you.

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:layout_weight="1">


        <TextView 
            android:id="@+id/tv_codProduto"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.3"     //Advertência aparece aqui
            android:text="Código Produto:" />

        <TextView 
            android:id="@+id/tv_descProduto"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.7"
            android:text="Descrição:" />
    </LinearLayout>

    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:layout_weight="1">

        <EditText 
            android:id="@+id/et_codProduto"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.3"           //Advertência aparece aqui
            android:inputType="none"/>

        <EditText 
            android:id="@+id/et_descProduto"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.7"
            android:inputType="none"/>
    </LinearLayout>

    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView 
            android:id="@+id/tv_complemento"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Complemento:"/>
        <EditText
            android:id="@+id/ed_complemento"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:inputType="none"/>
    </LinearLayout>

    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:layout_weight="1">

        <TextView 
            android:id="@+id/tv_qtdeEstoque"
            android:layout_weight="0.5"          //Advertência aparece aqui
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Estoque:"
            android:inputType="none"/>
        <TextView 
            android:id="@+id/tv_vrVenda"
            android:layout_weight="0.5"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Valor de Venda:"/>
    </LinearLayout>

    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:layout_weight="1">

<EditText
            android:id="@+id/ed_qtdeEstoque"
            android:layout_width="0dp"
            android:layout_weight="0.5"          //Advertência aparece aqui
            android:layout_height="wrap_content"
            android:inputType="none"/>

        <EditText
            android:id="@+id/ed_vrVenda"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:ems="10" />

    </LinearLayout>

    <LinearLayout 
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView 
            android:id="@+id/tv_fornecedor"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Fornecedor:"/>
        <EditText 
            android:id="@+id/ed_fornecedor"
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:inputType="none"/>
    </LinearLayout>
    </LinearLayout>

  • If the Eclipse returns with this warning is because it is actually bad for performance. I do not know why. What I can say is that in the many layouts I’ve done, I’ve rarely had to use android:layout_weight and, as I recall, never in a "nested" way. What this warning may also suggest is that there are other ways to achieve the same objective without the use of nested Weights. If you post the Xml of your layout it may be easier to help you. If not, this question may be considered Too broad

  • @ramaral - Add xml. Thanks for the help.

1 answer

1


That’s bad because the layout_weight requires that the widget is measured twice when a LinearLayout with Weight other than zero is nestled in another LinearLayout with Weight also different from zero, this exponentially increases the measurements.

An alternative is to use RelativeLayouts and adjust the display according to other places views , without the use of values dpi specific.

References:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.