布局文件
android:layout_width="match_parent"
android:layout_height="match_parent"
android:rowCount="6"
android:columnCount="4"
android:id="@+id/grid">
android: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="@android:color/white"
android:textColor="@android:color/black"
android:text="0"/>
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_columnSpan="4"
android:textSize="40sp"
android:text="清除"/>
源码
GridLayoutgridLayout;
String[]chars=newString[]{
"7","8","9","+",
"4","5","6","-",
"1","2","3","*",
".","0","=","/",
};
@Override
protected voidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridLayout= (GridLayout)findViewById(R.id.grid);
for(inti =0;i
Button bn =newButton(this);
bn.setText(chars[i]);
bn.setTextSize(40);
GridLayout.Spec rowSpec = GridLayout.spec((i /4)+2);
GridLayout.Spec columnSpec = GridLayout.spec(i %4);
GridLayout.LayoutParams params =newGridLayout.LayoutParams(rowSpec,columnSpec);
gridLayout.addView(bn,params);
}
}