How in Android to make two columns of buttons that will fill the screen, depending on its size? Can this be done only in the .xml file or do I need to write code in .java?
2 answers
The easiest option is to make a horizontal LinearLayout and set the buttons not for width, but for weight. And this LinearLayout need to put in the root. For the root, you can take a vertical LinearLayout .
|
It is best to use GridLayout since LinearLayout requires a lot of resources from the device. In GridLayout you specify the required number of rows and columns. Here is an example:
android.support.v7.widget.GridLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".Activity" app:columnCount="2" app:rowCount="5" Below are the elements that you fill out the layout. To stretch to full screen, all elements need to specify the following characteristics:
android:layout_height="0dp" android:layout_width="0dp" app:layout_columnWeight="1" app:layout_rowWeight="1" Here is a good description of how to work with GridLayout.
|