您的位置:首页 > 移动开发 > Android开发

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

2014-08-16 22:55 232 查看
我们知道计算器的制作基本上会用到表格布局来操作比较简单,它会把内容以行和列的形式来布局,简单方便,每一行用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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: