目录
案例案演示——计算器界面
- 运行效果
1、创建安卓应用
- 选择模板
- 完善项目信息
2、添加背景图片
3、编写主布局资源文件
- 源代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:background="@drawable/background"
android:gravity="center_horizontal"
android:orientation="vertical"
android:padding="15dp"
tools:context=".MainActivity">
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:columnCount="5"
android:rowCount="7">
<TextView
android:layout_width="345dp"
android:layout_height="50dp"
android:layout_row="0"
android:layout_columnSpan="5"
android:layout_marginBottom="30dp"
android:background="#DCDCDC"
android:gravity="right"
android:paddingRight="10dp"
android:text="0123456789"
android:textColor="@color/black"
android:textSize="25sp" />
<Button
android:layout_width="65dp"
android:layout_row="1"
android:layout_column="0"
android:backgroundTint="#DCDCDC"
android:text="MC"
android:textColor="@color/black" />
<Button
android:layout_width="65dp"
android:layout_row="1"
android:layout_column="1"
android:layout_marginLeft="5dp"
android:backgroundTint="#DCDCDC"
android:text="MR"
android:textColor="@color/black" />
<Button
android:layout_width="65dp"
android:layout_row="1"
android:layout_column="2"
android:layout_marginLeft="5dp"
android:backgroundTint="#DCDCDC"
android:text="MS"
android:textColor="@color/black" />
<Button
android:layout_width="65dp"
android:layout_row="1"
android:layout_column="3"
android:layout_marginLeft="5dp"
android:backgroundTint="#DCDCDC"
android:text="M+"
android:textColor="@color/black" />
<Button
android:layout_width="65dp"
android:layout_row="1"
android:layout_column="4"
android:layout_marginLeft="5dp"
android:backgroundTint="#DCDCDC"
android:text="M-"
android:textColor="@color/black" />
<Button
android:layout_width="65dp"
android:layout_row="2"
android:layout_column="0"
android:backgroundTint="#DCDCDC"
android:text="←"
android:textColor="@color/black" />
<Button
android:layout_width="65dp"
android:layout_row="2"
android:layout_column="1"
android:layout_marginLeft="5dp"
android:backgroundTint="#DCDCDC"
android:text="CE"
android:textColor="@color/black" />
<Button
android:layout_width="65dp"
android:layout_row="2"
android:layout_column="2"
android:layout_marginLeft="5dp"
android:backgroundTint="#DCDCDC"
android:text="C"
android:textColor="@color/black" />
<Button
android:layout_width="65dp"
android:layout_row="2"
android:layout_column="3"
android:layout_marginLeft="5dp"
android:backgroundTint="#DCDCDC"
android:text="±"
android:textColor="@color/black" />
<Button
android:layout_width="65dp"
android:layout_row="2"
android:layout_column="4"
android:layout_marginLeft="5dp"
android:backgroundTint="#DCDCDC"
android:text="√"
android:textColor="@color/black" />
<Button
android:layout_width="65dp"
android:layout_row="3"
android:layout_column="0"
android:backgroundTint="#DCDCDC"
android:text="7"
android:textColor="@color/black" />
<Button
android:layout_width="65dp"
android:layout_row="3"
android:layout_column="1"
android:layout_marginLeft="5dp"
android:backgroundTint="#DCDCDC"
android:text="8"
android:textColor="@color/black" />
<Button
android:layout_width="65dp"
android:layout_row="3"
android:layout_column="2"
android:layout_marginLeft="5dp"
android:backgroundTint="#DCDCDC"
android:text="9"
android:textColor="@color/black" />
<Button
android:layout_width="65dp"
android:layout_row="3"
android:layout_column="3"
android:layout_marginLeft="5dp"
android:backgroundTint="#DCDCDC"
android:text="/"
android:textColor="@color/black" />
<Button
android:layout_width="65dp"
android:layout_row="3"
android:layout_column="4"
android:layout_marginLeft="5dp"
android:backgroundTint="#DCDCDC"
android:text="%"
android:textColor="@color/black" />
<Button
android:layout_width="65dp"
android:layout_row="4"
android:layout_column="0"
android:backgroundTint="#DCDCDC"
android:text="4"
android:textColor="@color/black" />
<Button
android:layout_width="65dp"
android:layout_row="4"
android:layout_column="1"
android:layout_marginLeft="5dp"
android:backgroundTint="#DCDCDC"
android:text="5"
android:textColor="@color/black" />
<Button
android:layout_width="65dp"
android:layout_row="4"
android:layout_column="2"
android:layout_marginLeft="5dp"
android:backgroundTint="#DCDCDC"
android:text="6"
android:textColor="@color/black" />
<Button
android:layout_width="65dp"
android:layout_row="4"
android:layout_column="3"
android:layout_marginLeft="5dp"
android:backgroundTint="#DCDCDC"
android:text="*"
android:textColor="@color/black" />
<Button
android:layout_width="65dp"
android:layout_row="4"
android:layout_column="4"
android:layout_marginLeft="5dp"
android:backgroundTint="#DCDCDC"
android:text="1/x"
android:textColor="@color/black" />
<Button
android:layout_width="65dp"
android:layout_row="5"
android:layout_column="0"
android:backgroundTint="#DCDCDC"
android:text="1"
android:textColor="@color/black" />
<Button
android:layout_width="65dp"
android:layout_row="5"
android:layout_column="1"
android:layout_marginLeft="5dp"
android:backgroundTint="#DCDCDC"
android:text="2"
android:textColor="@color/black" />
<Button
android:layout_width="65dp"
android:layout_row="5"
android:layout_column="2"
android:layout_marginLeft="5dp"
android:backgroundTint="#DCDCDC"
android:text="3"
android:textColor="@color/black" />
<Button
android:layout_width="65dp"
android:layout_row="5"
android:layout_column="3"
android:layout_marginLeft="5dp"
android:backgroundTint="#DCDCDC"
android:text="-"
android:textColor="@color/black" />
<Button
android:layout_width="65dp"
android:layout_height="95dp"
android:layout_row="5"
android:layout_rowSpan="2"
android:layout_column="4"
android:layout_marginLeft="5dp"
android:backgroundTint="#DCDCDC"
android:text="="
android:textColor="@color/black" />
<Button
android:layout_width="135dp"
android:layout_row="6"
android:layout_column="0"
android:layout_columnSpan="2"
android:backgroundTint="#DCDCDC"
android:text="0"
android:textColor="@color/black" />
<Button
android:layout_width="65dp"
android:layout_row="6"
android:layout_column="2"
android:layout_marginLeft="5dp"
android:backgroundTint="#DCDCDC"
android:text="."
android:textColor="@color/black" />
<Button
android:layout_width="65dp"
android:layout_row="6"
android:layout_column="3"
android:layout_marginLeft="5dp"
android:backgroundTint="#DCDCDC"
android:text="+"
android:textColor="@color/black" />
</GridLayout>
</LinearLayout>