To write an example of a calculator, the layout file is as follows:
<? XML version= "1.0" encoding= "Utf-8" ?> < xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:layout_gravity= "Center"Android:usedefaultmargins= "true"Android:columncount= "4" > <ButtonAndroid:text= "@string/zero"Android:background= "#DFDFDF"android:textsize= "30SP"Android:layout_columnspan= "4"android:gravity= "Center_vertical|right"Android:paddingright= "8DP"android:layout_gravity= "Fill"android:clickable= "false" /> <ButtonAndroid:text= "@string/clear"Android:layout_columnspan= "3"android:layout_gravity= "Fill" /> <ButtonAndroid:text= "@string/divide"android:layout_gravity= "Fill" /> <ButtonAndroid:text= "@string/one"android:layout_gravity= "Fill" /> <ButtonAndroid:text= "@string/two"android:layout_gravity= "Fill" /> <ButtonAndroid:text= "@string/three"android:layout_gravity= "Fill" /> <ButtonAndroid:text= "@string/multiply"android:layout_gravity= "Fill" /> <ButtonAndroid:text= "@string/four"android:layout_gravity= "Fill" /> <ButtonAndroid:text= "@string/five"android:layout_gravity= "Fill" /> <ButtonAndroid:text= "@string/six"android:layout_gravity= "Fill" /> <ButtonAndroid:text= "@string/sub"android:layout_gravity= "Fill" /> <ButtonAndroid:text= "@string/seven"android:layout_gravity= "Fill" /> <ButtonAndroid:text= "@string/eight"android:layout_gravity= "Fill" /> <ButtonAndroid:text= "@string/nine"android:layout_gravity= "Fill" /> <ButtonAndroid:text= "@string/add"Android:layout_rowspan= "3"android:layout_gravity= "Fill" /> <ButtonAndroid:text= "@string/zero"Android:layout_columnspan= "2"android:layout_gravity= "Fill" /> <ButtonAndroid:text= "@string/dot"android:layout_gravity= "Fill" /> <ButtonAndroid:text= "@string/equal"Android:layout_columnspan= "3"android:layout_gravity= "Fill" /> </GridLayout>
Where the string file is Res/values/string.xml:
<?XML version= "1.0" encoding= "Utf-8"?><Resources> <stringname= "App_name">My Second App</string> <stringname= "Hello_world">Hello world!</string> <stringname= "Action_settings">Settings</string> <stringname= "Str_hello">Hello, Android!</string> <stringname= "Calc_title">Calculator</string> <stringname= "Zero">0</string> <stringname= "One">1</string> <stringname= "both">2</string> <stringname= "three">3</string> <stringname= "Four">4</string> <stringname= "Five">5</string> <stringname= "Six">6</string> <stringname= "Seven">7</string> <stringname= "Eight">8</string> <stringname= "Nine">9</string> <stringname= "dot">.</string> <stringname= "Add">+</string> <stringname= "Sub">-</string> <stringname= "Multiply">*</string> <stringname= "Divide">/</string> <stringname= "equal">=</string> <stringname= "Clear">C</string></Resources>
The result of the operation is:
The results shown in the last row (line 7th) and the last column (column 4th) are not imagined.
Android Starter Series: UI components: Layout Manager: GridLayout