- 这是一个Android的计算器界面布局,仅布局,不实现功能
目录
1. 界面布局分析
- 效果图
- 这个界面总体是垂直分布,从上到下共7行
- 第1、2行是输入、输出框,这里采用的是EditText
- 第3到7行是输入按钮,每行水平分布
- 第3到6行行内每个Button大小相同
- 第7行的Button“0”占了两个位置
由此我们可以简单总结出,总框架权重设置为7,每行占总权重的1,但是按钮行的按钮个数、大小不一样,要使界面美观的话必须也要行内对齐,所以每行也要单独设置权重
2. 实现代码及注释
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" #垂直分布
android:weightSum="7"> #总权重为7
<EditText #第一个输入框
android:id="@+id/input"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#aaaaaa"
android:layout_weight="1"/> #占总权重7的1(7.1)
<EditText #第二个文本框
android:id="@+id/output"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#cccccc"
android:layout_weight="1"/> #占总权重7的1(7.2)
<LinearLayout #第一行Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" #此行占总权重的1(7.3)
android:orientation="horizontal" #控件内容水平排列
android:weightSum="5"> #控件有5个button,设置行权重5
<Button #第一行第一个Button
android:id="@+id/button_clear"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" #占父LinearLayout权重的1(5.1)
android:text="AC"
android:textSize="25sp" />
<Button #第一行第二个Button
android:id="@+id/button_delete"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" #占父LinearLayout权重的1(5.2)