Calculator interface and calculator Interface Design
Demo2 \ jisuanqi \ src \ main \ res \ layout \ activity_main.xml
1 <GridLayout xmlns: android = "http://schemas.android.com/apk/res/android" 2 xmlns: tools = "http://schemas.android.com/tools" 3 android: layout_width = "match_parent" 4 android: layout_height = "match_parent" 5 tools: context = ". mainActivity "6 android: id =" @ + id/root "7 android: rowCount =" 6 "8 android: columnCount =" 4 "> 9 <! -- Define a text box that spans 4 columns --> 10 <TextView11 android: layout_width = "match_parent" 12 android: layout_height = "wrap_content" 13 android: layout_columnSpan = "4" 14 android: textSize = "50sp" 15 android: layout_marginLeft = "2dp" 16 android: layout_marginRight = "2dp" 17 android: padding = "3dp" 18 android: layout_gravity = "right" 19 android: background = "# eee" 20 android: textColor = "#000" 21 android: text = "0"/> 22 <! -- Define a button that spans 4 columns --> 23 <Button24 android: layout_width = "match_parent" 25 android: layout_height = "wrap_content" 26 android: layout_columnSpan = "4" 27 android: text = "clear"/> 28 29 30 </GridLayout>
Demo2 \ jisuanqi \ src \ main \ java \ com \ ly \ jisuanqi \ MainActivity. java
1 import android. app. activity; 2 import android. OS. bundle; 3 import android. view. gravity; 4 import android. widget. button; 5 import android. widget. gridLayout; 6 7 public class MainActivity extends Activity {8 private GridLayout gridLayout; 9 // define the text 10 private String [] chars = new String [] {11 "7", "8", "9 ","/", 12 "4", "5", "6", "*", 13 "1", "2", "3", "-", 14 ". "," 0 "," = "," + ", 15}; 16 private Button bn; 17 @ Override18 protected void onCreate (Bundle savedInstanceState) {19 super. onCreate (savedInstanceState); 20 setContentView (R. layout. activity_main); 21 gridLayout = (GridLayout) findViewById (R. id. root); 22 for (int I = 0; I <chars. length; I ++) {23 bn = new Button (this); 24 bn. setText (chars [I]); 25 // set the font size of the button to 26 bn. setTextSize (40); 27 // set the blank area around the button 28 bn. setPadding (5, 35, 5, 35); 29 // specify the row where the component is located as 30 GridLayout. spec rowSpec = GridLayout. spec (I/4 + 2); 31 // specify the 32 GridLayout column of the component. spec columnSpec = GridLayout. spec (I % 4); 33 GridLayout. layoutParams params = new GridLayout. layoutParams (rowSpec, columnSpec); 34 // specify that the component occupies 35 params of the parent container. setGravity (Gravity. FILL); 36 gridLayout. addView (bn, params); 37} 38} 39 40 41}