Android利用布局来制作一个简易计算器

我们知道计算器的制作基本上会用到表格布局来操作比较简单,它会把内容以行和列的形式来布局,简单方便,每一行用tableRow表示,如果要跨行或跨列的话要用到android:layout_span="2"表示这一列要跨两列

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >


   <TableRow 
       android:layout_width="wrap_content"
       android:layout_height="wrap_content">
       <Button 
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="1"/>
       <Button 
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="2"/>
       <Button 
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="3"/>
       <Button 
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="+"/>
   </TableRow>
    <TableRow 
        android:layout_width="wrap_content"
       android:layout_height="wrap_content">
       <Button 
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="4"/>
       <Button 
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="5"/>
       <Button 
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="6"/>
       <Button 
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="-"/>
   </TableRow>
   <TableRow 
       android:layout_width="wrap_content"
       android:layout_height="wrap_content">
       <Button 
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="7"/>
       <Button 
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="8"/>
       <Button 
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="9"/>
       <Button 
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="*"/>
   </TableRow>
   <TableRow 
       android:layout_width="wrap_content"
       android:layout_height="wrap_content">
       <Button 
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="0"/>
       <Button 
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="="
       android:layout_span="2"/>
       <Button 
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="/"/>
</TableRow>
</TableLayout>


这样一个简单的计算器就完成了,但是用表格布局tablelayout也是存在问题,这种布局可能会出现不能将控件占据多个行或列的问题,而且渲染速度也不能得到很好的保证。

所以在Android4.0之后我们用到了网格布局gridlayout, 若要指定某控件显示在固定的行或列,只需设置该子控件的android:layout_row和android:layout_column属性即可,但是需要注意:android:layout_row=”0”表示从第一行开始,android:layout_column=”0”表示从第一列开始,如果需要设置某控件跨越多行或多列,只需将该子控件android:layout_rowSpan或者layout_columnSpan属性设置为数值,再设置其layout_gravity属性为fill即可,前一个设置表明该控件跨越的行数或列数,后一个设置表明该控件填满所跨越的整行或整列。

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" 
    android:rowCount="4"
    android:columnCount="4"
    android:orientation="horizontal">


    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="0"
        android:layout_column="0"
        android:text="1"/>
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="0"
        android:layout_column="1"
        android:text="2"/>
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="0"
        android:layout_column="2"
        android:text="3"/>
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="0"
        android:layout_column="3"
        android:text="+"/>
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="1"
        android:layout_column="0"
        android:text="4"/>
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="1"
        android:layout_column="1"
        android:text="5"/>
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="1"
        android:layout_column="2"
        android:text="6"/>
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="1"
        android:layout_column="3"
        android:text="-"/>
     <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="2"
        android:layout_column="0"
        android:text="7"/>
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="2"
        android:layout_column="1"
        android:text="8"/>
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="2"
        android:layout_column="2"
        android:text="9"/>
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="2"
        android:layout_column="3"
        android:text="*"/>
     <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="3"
        android:layout_column="0"
        android:text="0"/>
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="3"
        android:layout_column="1"
        android:layout_columnSpan="2"
        android:layout_gravity="fill_horizontal"
        android:text="="/>
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="3"
        android:layout_column="3"
        android:text="/"/>
   
    
</GridLayout>

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值