1 PackageCom.metrox.absolutelayoutdemo;2 3 Importandroid.support.v7.app.AppCompatActivity;4 ImportAndroid.os.Bundle;5 Importandroid.view.Gravity;6 ImportAndroid.widget.Button;7 Importandroid.widget.GridLayout;8 9 Public classMainactivityextendsappcompatactivity {Ten OneString[] chars =Newstring[]{ A"7", "8", "9", "÷", -"4", "5", "6", "X", -"1", "2", "3", "-", the".", "0", "=", "+" - }; - @Override - protected voidonCreate (Bundle savedinstancestate) { + Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main); +GridLayout DemoGrid =(GridLayout) Findviewbyid (r.id.root); A for(inti = 0;i<chars.length;i++){ atButton bn =NewButton ( This); - Bn.settext (Chars[i]); -Bn.settextsize (40); -Bn.setpadding (5, 30, 5, 30); -Gridlayout.spec Rowspec = Gridlayout.spec (I/4 + 2); -Gridlayout.spec Columnspec = gridlayout.spec (i% 4); inGridlayout.layoutparams params =NewGridlayout.layoutparams (rowspec,columnspec); - params.setgravity (Gravity.fill); to Demogrid.addview (bn,params); + } - } the}
1 <?XML version= "1.0" encoding= "Utf-8"?>2 <Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"3 Xmlns:tools= "Http://schemas.android.com/tools"4 Android:layout_width= "Match_parent"5 Android:layout_height= "Match_parent"6 Android:paddingbottom= "@dimen/activity_vertical_margin"7 Android:paddingleft= "@dimen/activity_horizontal_margin"8 Android:paddingright= "@dimen/activity_horizontal_margin"9 Android:paddingtop= "@dimen/activity_vertical_margin"Ten Tools:context= "Com.metrox.absolutelayoutdemo.MainActivity"> One A <GridLayout - Android:layout_width= "Match_parent" - Android:layout_height= "Match_parent" the Android:columncount= "4" - Android:rowcount= "6" - Android:id= "@+id/root"> - + <TextView - Android:layout_width= "Match_parent" + Android:layout_height= "Wrap_content" A Android:text= "0" at Android:id= "@+id/textview" - Android:layout_columnspan= "4" - Android:layout_marginleft= "2pt" - Android:layout_marginright= "2pt" - android:padding= "3pt" - android:gravity= "Right" in android:textsize= "35SP" - Android:background= "#eee" to Android:textcolor= "#000" /> + - <Button the Android:layout_width= "Match_parent" * Android:layout_height= "Wrap_content" $ Android:text= "Clear"Panax Notoginseng Android:layout_columnspan= "4" - Android:id= "@+id/button" /> the </GridLayout> + </Relativelayout>
<?XML version= "1.0" encoding= "Utf-8"?><Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Com.metrox.absolutelayoutdemo"> <ApplicationAndroid:allowbackup= "true"Android:icon= "@mipmap/ic_launcher"Android:label= "@string/app_name"Android:supportsrtl= "true"Android:theme= "@style/apptheme"> <ActivityAndroid:name=". Mainactivity "> <Intent-filter> <ActionAndroid:name= "Android.intent.action.MAIN" /> <categoryAndroid:name= "Android.intent.category.LAUNCHER" /> </Intent-filter> </Activity> </Application></Manifest>
Android Dynamically Create controls