I propose the following solution (here it is six cells, but you can add any number you need):
Reject RelativeLayout as it is a fairly resource-intensive container that is not needed here, in favor of the TableLayout most suitable for this markup with TableLayout columns ( android:stretchColumns="*" ). Use ImageView instead of ImageButton , so it does not have a default background. Here, for all ImageView , one selector resource selectable_image1.xml is set as an image, you will naturally make your own cell for each cell, with different pictures:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TableLayout android:layout_width="match_parent" android:layout_height="match_parent" android:isScrollContainer="true" android:stretchColumns="*"> <TableRow android:layout_width="match_parent" android:layout_height="match_parent" android:paddingTop="30dp"> <ImageView android:id="@+id/imageView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:adjustViewBounds="true" android:clickable="true" android:src="@drawable/selectable_image1" /> <ImageView android:id="@+id/imageView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:adjustViewBounds="true" android:clickable="true" android:src="@drawable/selectable_image1" /> </TableRow> <TableRow android:layout_width="match_parent" android:layout_height="match_parent" android:paddingTop="30dp"> <ImageView android:id="@+id/imageView5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:adjustViewBounds="true" android:clickable="true" android:src="@drawable/selectable_image1" /> <ImageView android:id="@+id/imageView4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:adjustViewBounds="true" android:clickable="true" android:src="@drawable/selectable_image1" /> </TableRow> <TableRow android:layout_width="match_parent" android:layout_height="match_parent" android:paddingTop="30dp"> <ImageView android:id="@+id/imageView7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:adjustViewBounds="true" android:clickable="true" android:src="@drawable/selectable_image1" /> <ImageView android:id="@+id/imageView6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:adjustViewBounds="true" android:clickable="true" android:src="@drawable/selectable_image1" /> </TableRow> </TableLayout> </ScrollView>
the android:paddingTop="30dp" in the tableRow elements sets the distance between rows.
The images themselves. In this case, I think it would be appropriate to use an image of the same size for all screen densities ( hdpi , xhdpi , etc.), with its own pixel size sufficient for high-quality display on xhdpi screens, on other screens it will be scaled using the android:adjustViewBounds="true" parameter android:adjustViewBounds="true" for ImageView . We set the parameter for android:clickable="true" for ImageView so that it reacts to clicks.
To make the response to pressing, I suggest not to take another set of images, but simply add a frame to the already existing images, this will reduce the size of the application.
We make a resource for the pressed state. On the image we impose a frame (rectangle) 5dp thick with roundings of 10dp (you can choose your own values). With the background of the fill of the entire rectangle, we specify transparent - # 0000 so that the image can be seen and only the frame is displayed. Let's call it press_image1.xml . We place in the res / drawable / folder:
press_image1.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item> <bitmap android:src="@mipmap/image1" /> </item> <item> <shape android:shape="rectangle"> <corners android:radius="10dp" /> <solid android:color="#0000"/> <stroke android:width="5dip" android:color="#3dbc24" /> </shape> </item> </layer-list>
Where image1 is a cell image.
Make a selector to react to clicks, let's call it selectable_image1.xml , put it in the / res / drawable / folder / This selector will be the image source in the ImageView , which we specify using the android:src parameter android:src :
selectable_image1.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@drawable/press_image1" /> <item android:drawable="@mipmap/image1" /> </selector>
Well that's all. You will need to do a similar job for each picture.
android:srcattribute, if you need the icon to be different when you click or assign your selector forandroid:src, and not for the background, do not specify the background - depending on what result you need to get - pavlofffandroid:src="@mipmap/ic_wok_ovosh_7na8"parameter from theImageViewattributesandroid:src="@mipmap/ic_wok_ovosh_7na8", what happens? - pavlofff