安卓学习之利用Tablelayout实现简单计算器布局
最近因为有安卓开发的课,加上自己还有一个与安卓有关的大创项目,于是开始自学起了安卓。总得来说,安卓开发还是挺有意思的,如果有对这方面感兴趣的话推荐学习一下哦!
最近刚好老师布置了个作业题,感觉做这个蛮有意思的 。
哈哈,废话少说,直入主题! (⊙v⊙)
这个是效果图
这个是对应的布局xml文件内容
- 1 <?xml version="1.0" encoding="utf-8"?>
- 2 <TableLayout
- 3 xmlns:android="http://schemas.android.com/apk/res/android"
- 4 xmlns:app="http://schemas.android.com/apk/res-auto"
- 5 xmlns:tools="http://schemas.android.com/tools"
- 6 android:layout_width="match_parent"
- 7 android:layout_height="match_parent"
- 8 android:gravity="center_vertical">
- 9
- 10 <TextView
- 11 android:layout_span="2"
- 12 android:layout_width="match_parent"
- 13 android:layout_height="100dp"
- 14 android:background="@mipmap/backgroundcolor"
- 15 android:hint="1000"
- 16 android:textSize="80dp"
- 17 android:gravity="right"/>
- 18 <TextView />
- 19 <TableRow
- 20 android:layout_marginTop="2dp"
- 21 android:layout_width="wrap_content"
- 22 android:layout_height="wrap_content"
- 23 android:gravity="fill_horizontal">
- 24 <Button
- 25 android:layout_weight="1"
- 26 android:layout_width="wrap_content"
- 27 android:layout_height="match_parent"
- 28 android:text="ON"/>
- 29 <Button
- 30 android:layout_weight="1"
- 31 android:layout_width="wrap_content"
- 32 android:layout_height="match_parent"
- 33 android:text="OFF"/>
- 34 <Button
- 35 android:layout_weight="1"
- 36 android:layout_width="wrap_content"
- 37 android:layout_height="match_parent"
- 38 android:text="M+"/>
- 39 <Button
- 40 android:layout_weight="1"
- 41 android:layout_width="wrap_content"
- 42 android:layout_height="match_parent"
- 43 android:text="M-"/>
- 44 </TableRow>
- 45 <TableRow
- 46 android:layout_width="wrap_content"
- 47 android:layout_height="wrap_content"
- 48 android:gravity="fill_horizontal">
- 49 <Button
- 50 android:layout_weight="1"
- 51 android:layout_width="wrap_content"
- 52 android:layout_height="match_parent"
- 53 android:text="AC"/>
- 54 <Button
- 55 android:layout_weight="1"
- 56 android:layout_width="wrap_content"
- 57 android:layout_height="match_parent"
- 58 android:text="+/-"/>
- 59 <Button
- 60 android:layout_weight="1"
- 61 android:layout_width="wrap_content"
- 62 android:layout_height="match_parent"
- 63 android:text="DEL"/>
- 64 <Button
- 65 android:layout_weight="1"
- 66 android:layout_width="wrap_content"
- 67 android:layout_height="match_parent"
- 68 android:text="%"/>
- 69 </TableRow>
- 70 <TableRow
- 71 android:layout_width="wrap_content"
- 72 android:layout_height="wrap_content"
- 73 android:gravity="fill_horizontal">
- 74 <Button
- 75 android:layout_weight="1"
- 76 android:layout_width="wrap_content"
- 77 android:layout_height="match_parent"
- 78 android:text="1"/>
- 79 <Button
- 80 android:layout_weight="1"
- 81 android:layout_width="wrap_content"
- 82 android:layout_height="match_parent"
- 83 android:text="2"/>
- 84 <Button
- 85 android:layout_weight="1"
- 86 android:layout_width="wrap_content"
- 87 android:layout_height="match_parent"
- 88 android:text="3"/>
- 89 <Button
- 90 android:layout_weight="1"
- 91 android:layout_width="wrap_content"
- 92 android:layout_height="match_parent"
- 93 android:text="/"/>
- 94 </TableRow>
- 95 <TableRow
- 96 android:layout_width="wrap_content"
- 97 android:layout_height="wrap_content"
- 98 android:gravity="fill_horizontal">
- 99 <Button
- 100 android:layout_weight="1"
- 101 android:layout_width="wrap_content"
- 102 android:layout_height="match_parent"
- 103 android:text="4"/>
- 104 <Button
- 105 android:layout_weight="1"
- 106 android:layout_width="wrap_content"
- 107 android:layout_height="match_parent"
- 108 android:text="5"/>
- 109 <Button
- 110 android:layout_weight="1"
- 111 android:layout_width="wrap_content"
- 112 android:layout_height="match_parent"
- 113 android:text="6"/>
- 114 <Button
- 115 android:layout_weight="1"
- 116 android:layout_width="wrap_content"
- 117 android:layout_height="match_parent"
- 118 android:text="×"/>
- 119 </TableRow>
- 120 <TableRow
- 121 android:layout_width="wrap_content"
- 122 android:layout_height="wrap_content"
- 123 android:gravity="fill_horizontal">
- 124 <Button
- 125 android:layout_weight="1"
- 126 android:layout_width="wrap_content"
- 127 android:layout_height="match_parent"
- 128 android:text="7"/>
- 129 <Button
- 130 android:layout_weight="1"
- 131 android:layout_width="wrap_content"
- 132 android:layout_height="match_parent"
- 133 android:text="8"/>
- 134 <Button
- 135 android:layout_weight="1"
- 136 android:layout_width="wrap_content"
- 137 android:layout_height="match_parent"
- 138 android:text="9"/>
- 139
- 140 <Button
- 141 android:layout_width="wrap_content"
- 142 android:layout_height="match_parent"
- 143 android:layout_weight="1"
- 144 android:text="-" />
- 145 </TableRow>
- 146 <TableRow
- 147 android:layout_width="wrap_content"
- 148 android:layout_height="wrap_content"
- 149 android:gravity="fill_horizontal">
- 150 <Button
- 151 android:layout_weight="1"
- 152 android:layout_width="wrap_content"
- 153 android:layout_height="match_parent"
- 154 android:text="0"/>
- 155 <Button
- 156 android:layout_weight="1"
- 157 android:layout_width="wrap_content"
- 158 android:layout_height="match_parent"
- 159 android:text="00"/>
- 160 <Button
- 161 android:layout_weight="1"
- 162 android:layout_width="wrap_content"
- 163 android:layout_height="match_parent"
- 164 android:text="."/>
- 165
- 166 <Button
- 167 android:layout_width="wrap_content"
- 168 android:layout_height="match_parent"
- 169 android:layout_weight="1"
- 170 android:text="+" />
- 171 </TableRow>
- 172 <Button
- 173 android:layout_width="match_parent"
- 174 android:layout_height="wrap_content"
- 175 android:text="="
- 176 android:textSize="30dp"/>
- 177 </TableLayout>
另外,为了实现计算器的“显示屏”的效果,我们还需要在res/mipmap里放下我们的Testview的背景图片!(这个可以自己做哦!)
(事情有些多,先给下效果和代码,后面有空就详细说下!^_^)