androidandroid-layoutandroid-relativelayoutnestedlayout

Android nested layouts


I am new to android layouts, Could please tell me how the performance of application when using nested layouts instead of using layout below, layout above, layout right, layout left attributes of relative layout for components.

For eg:

<RelativeLayout>
    <scrollView>
       <RelativeLayout>
          <LinearLayout> Header<LinearLayout>
           <TextView/>  <TextView/>
           <TableLayout>
                   <TableRow><ImageView/> <TextView> <Spinner><TableRow>
                   <TableRow><ImageView/> <TextView> <Spinner><TableRow>
                   <TableRow><ImageView/> <TextView> <Spinner><TableRow>
                   <TableRow><ImageView/> <TextView> <Spinner><TableRow>
          </TableLayout>

          <TextView>
          <TableLayout>
                 <TableRow>
                    <LinearLayout> <ImageView><TextView></LinearLayout> 
                    <EditText>
                 </TableRow>
                 <TableRow>
                    <LinearLayout> <ImageView><TextView></LinearLayout>
                    <EditText>
                </TableRow>
                <TableRow>
                   <TextView> 
                   < Spinner>
                </TableRow>
                <TableRow><TextView> <Spinner></TableRow>
          </TableLayout>

           <TableLayouts>---</TableLayout>

          <LinearLayout> footer</LinearLayout>
</RelativeLayout>
<ScrollView>
</RelativeLayout>

Thanks & Regards Yamini


Solution

  • Well some layouts can only be made by doing some level of nesting. But you should avoid having too many nested LinearLayouts and even more important NEVER nest LinearLayouts with weights. You can read a little more about optimizing layouts in the official docs.

    Personally I use LinearLayouts for simple stuff and start using RelativeLayout when the layout gets more complex. There is no one answer as it is a matter of preference.