Create a mesh like a layout in Android

Basically, I have a view that needs to be divided into a 2x4 grid, with each section being equal in size (i.e. each row will be 25% of the screen height, and each column will be 50% of the screen width).

My approach was to use a horizontal LinearLayout with two inner LinearLayouts with a weight of 0.5, and then in each of these LinearLayouts set the weight of the children to 0.25 so that each of them occupies 25% of the screen.

Although this seems to work, it seems to be very bad for performance (see this thread for the reason why Nested weights are bad for performance? Alternatives? )

Are there any alternatives to achieve this? I looked around, but I can not find a pure XML solution for it.

Below is an example code of how I have installed LinearLayouts and their children.

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:baselineAligned="false" android:weightSum="1.0" > <LinearLayout android:orientation="vertical" android:layout_width="0dip" android:layout_height="fill_parent" android:layout_weight="0.5" android:weightSum="1.0"> <ImageView android:layout_width="wrap_content" android:layout_height="0dip" android:src="@drawable/example" android:layout_gravity="center_horizontal" android:layout_weight="0.25" /> <ImageView android:layout_width="wrap_content" android:layout_height="0dip" android:src="@drawable/example" android:layout_gravity="center_horizontal" android:layout_weight="0.25" /> <ImageView android:layout_width="wrap_content" android:layout_height="0dip" android:src="@drawable/example" android:layout_gravity="center_horizontal" android:layout_weight="0.25" /> <ImageView android:layout_width="wrap_content" android:layout_height="0dip" android:src="@drawable/example" android:layout_gravity="center_horizontal" android:layout_weight="0.25" /> </LinearLayout> <LinearLayout android:orientation="vertical" android:layout_width="0dip" android:layout_height="fill_parent" android:layout_weight="0.5" android:weightSum="1.0"> <ImageView android:layout_width="wrap_content" android:layout_height="0dip" android:src="@drawable/example" android:layout_gravity="center_horizontal" android:layout_weight="0.25" /> <ImageView android:layout_width="wrap_content" android:layout_height="0dip" android:src="@drawable/example" android:layout_gravity="center_horizontal" android:layout_weight="0.25" /> <ImageView android:layout_width="wrap_content" android:layout_height="0dip" android:src="@drawable/example" android:layout_gravity="center_horizontal" android:layout_weight="0.25" /> <ImageView android:layout_width="wrap_content" android:layout_height="0dip" android:src="@drawable/example" android:layout_gravity="center_horizontal" android:layout_weight="0.25" /> </LinearLayout> </LinearLayout> 
+4
source share
2 answers

You might want to give GridLayout a back.

There is also a library that makes it available for devices with 1.6+.

+4
source
+1
source

Source: https://habr.com/ru/post/1411132/


All Articles