Package Com.example.gridlayout;import Android.app.activity;import android.os.bundle;import Android.view.gravity;import Android.widget.button;import android.widget.GridLayout; Public classMaingrid extends Activity {GridLayout grd; String[] Chas=Newstring[] {"7","8","9","*" ,"4","5","6","/" ,"1","2","3","+" ,"0",".","=","-" }; @Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main_grid); GRD=(GridLayout) Findviewbyid (r.id.root); for(intI=0; i<chas.length;i++) {Button bt=NewButton ( This); Bt.settext (Chas[i]); Bt.settextsize ( +); Bt.setheight ( $); Gridlayout.spec Rowspec= Gridlayout.spec (I/4+2); //specify the column in which the component is locatedGridlayout.spec Columnspec = gridlayout.spec (i%4); Gridlayout.layoutparamsparams=Newgridlayout.layoutparams (Rowspec, Columnspec); //Specifies that the component fills the parent container params. setgravity (Gravity.fill); Grd.addview (BT,params); } }}
<?XML version= "1.0" encoding= "Utf-8"?><GridLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:rowcount= "6"Android:columncount= "4"Android:id= "@+id/root" ><!--Define a text box that spans 4 columns and set the foreground color, background color, and other properties of the text box -<TextViewAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:layout_columnspan= "4"android:textsize= "50SP"Android:layout_marginleft= "4px"Android:layout_marginright= "4px"android:padding= "5px"android:layout_gravity= "Right"Android:background= "#eee"Android:textcolor= "#000"Android:text= "0"/><!--Define a button that spans 4 columns -<ButtonAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:layout_columnspan= "4"Android:text= "Clear"/></GridLayout>
<?XML version= "1.0" encoding= "Utf-8"?><Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Com.example.gridlayout"Android:versioncode= "1"Android:versionname= "1.0" > <USES-SDKandroid:minsdkversion= "+"android:targetsdkversion= "+" /> <ApplicationAndroid:allowbackup= "true"Android:icon= "@drawable/ic_launcher"Android:label= "@string/app_name"Android:theme= "@style/apptheme" > <ActivityAndroid:name=". Maingrid "Android:label= "@string/app_name" > <Intent-filter> <ActionAndroid:name= "Android.intent.action.MAIN" /> <categoryAndroid:name= "Android.intent.category.LAUNCHER" /> </Intent-filter> </Activity> </Application></Manifest>
Gridlaylout Simple Layout