美文网首页
计算器布局基于GridLayout

计算器布局基于GridLayout

作者: 飞飞小狮子Will | 来源:发表于2017-11-19 22:00 被阅读0次

    布局文件

    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);

    }

    }

    相关文章

      网友评论

          本文标题:计算器布局基于GridLayout

          本文链接:https://www.haomeiwen.com/subject/jvapvxtx.html