- 这是一个Android的计算器界面布局,仅布局,不实现功能


目录

  • 1. 界面布局分析
  • 2. 实现代码及注释
  • 3. 代码说明
  • 4. 参考资料

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                     #第一行Buttonandroid: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                                #第一行第一个Buttonandroid: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                              #第一行第二个Buttonandroid:id="@+id/button_delete"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"       #占父LinearLayout权重的1(5.2)android:text="DEL"android:textSize="25sp" /><Buttonandroid:id="@+id/button_left"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:text="("android:textSize="25sp" /><Buttonandroid:id="@+id/button_right"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:text=")"android:textSize="25sp" /><Button                                  #第一行第5个Buttonandroid:id="@+id/button_divide"       android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"            #占父LinearLayout权重的1(5.5)android:text="/"android:textSize="25sp" /></LinearLayout><LinearLayout                            #第二行Buttonandroid:layout_width="fill_parent"android:layout_height="0dp"android:layout_weight="1"           #占总权重的1(7.4)android:orientation="horizontal"   #控件内容水平排列android:weightSum="4">               #控件有4个button,设置行权重4<Buttonandroid:id="@+id/button_7"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"     #占父LinearLayout权重的1(4.1)android:text="7"android:textSize="25sp" /><Buttonandroid:id="@+id/button_8"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"        #占父LinearLayout权重的1(4.2)android:text="8"android:textSize="25sp" /><Buttonandroid:id="@+id/button_9"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"        #占父LinearLayout权重的1(4.3)android:text="9"android:textSize="25sp" /><Buttonandroid:id="@+id/button_add"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"      #占父LinearLayout权重的1(4.4)android:text="+"android:textSize="50sp" /></LinearLayout><LinearLayout                           #第三行Buttonandroid:layout_width="fill_parent"android:layout_height="0dp"android:layout_weight="1"           #占总权重的1(7.5)android:orientation="horizontal"android:weightSum="4">               #此控件包含4个Button,设置行权重4<Buttonandroid:id="@+id/button_4"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"       #占父LinearLayout权重的1(4.1)android:text="4"android:textSize="25sp" /><Buttonandroid:id="@+id/button_5"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"        #占父LinearLayout权重的1(4.2)android:text="5"android:textSize="25sp" /><Buttonandroid:id="@+id/button_6"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"        #占父LinearLayout权重的1(4.3)android:text="6"android:textSize="25sp" /><Buttonandroid:id="@+id/button_minus"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"        #占父LinearLayout权重的1(4.4)android:text="-"android:textSize="25sp" /></LinearLayout><LinearLayout                            #第四行Buttonandroid:layout_width="fill_parent"android:layout_height="0dp"android:layout_weight="1"           #占总权重的1(7.6)android:orientation="horizontal"android:weightSum="4">               #此控件包含4个Button,设置行权重4<Buttonandroid:id="@+id/button_1"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:text="1"android:textSize="25sp" /><Buttonandroid:id="@+id/button_2"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:text="2"android:textSize="25sp" /><Buttonandroid:id="@+id/button_3"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:text="3"android:textSize="25sp" /><Buttonandroid:id="@+id/button_mult"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:text="x"android:textSize="25sp" /></LinearLayout><LinearLayout                            #第五行Button,特殊android:layout_width="fill_parent"android:layout_height="0dp"android:layout_weight="1"android:orientation="horizontal"android:weightSum="4">              #“0”占了两个位置,其余两个按钮分别占了一个位置,设置行权重为4<Buttonandroid:id="@+id/button_0"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="2"        #“0”占了两个位置,占父LinearLayout权重的2(4.2)android:text="0"android:textSize="25sp" /><Buttonandroid:id="@+id/button_point"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"     #“.”占了1个位置,占父LinearLayout权重的1(4.3)android:text="."android:textSize="25sp" /><Buttonandroid:id="@+id/button_equal"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"     #占了1个位置,占父LinearLayout权重的1(4.4)android:text="="android:textSize="25sp" /></LinearLayout>
</LinearLayout>

3. 代码说明

  • 这个布局主要考验对权重 android:layout_weight 的理解,可以简单粗暴的理解为主控件 android:weightSum 设置总权重,旗下每个控件的 android:layout_weight 表示子控件占父控件的空间比例
  • 这个例子中的主控件是最外层的 LinearLayout,旗下包含7个子控件,两个 EditText 用于显示输入输出,5个子 LinearLayout 用于显示每行的按钮,所以设置主控件的权重为7,每个子控件各占1
  • 在子控件 LinearLayout 中,前4个 LinearLayout 中的 Button 都是均匀分布,大小相同的,所以有几个 Button 就设置父 LinearLayout 的权重 android:weightSum 为几,然后每个 Button 所占的权重 android:layout_weight 为1,效果就是每行 LinearLayout 内的 Button 均匀排列
  • 最后一行的 LinearLayout 中,Button “0” 占了两个位置,其它两个 Button 各占一个位置,共4个位置大小,所以设置父 LinearLayout 的权重 android:weightSum 为4,Button ”0“ 的 android:layout_weight 为2,其余两个 Buttonandroid:layout_weight 均为1
  • android:layout_weight 对应属性的属性值设置为0dp,此例中主控件 LinearLayout 的子控件为垂直排列,因此子控件 EditText 和子控件 LinearLayoutandroid:layout_height 的值都是0dp; 在子控件 LinearLayout 中,Button 为水平摆列,因此 Buttonandroid:layout_width 的值是0dp

4. 参考资料

  • layout_weight深刻理解

Android计算器界面布局相关推荐

  1. eclipse完成Android计算器界面开发,并且实现基本的功能(加减乘除、清零)

    eclipse完成Android计算器界面开发,并且实现基本的功能(加减乘除.清零) 先布局好计算器的结构样式,再编写java文件,如下: activity_main.xml代码 <Linear ...

  2. Android常见界面布局

    第2章 Android常见界面布局 第2章 Android常见界面布局 2.1 View视图 2.2 界面布局编写方式 2.2.1 在XML文件中编写布局 2.2.2 在Java代码中编写布局 2.3 ...

  3. Android常见界面布局(详细介绍)

    一.View视图 所有的UI元素都是通过View与ViewGroup构建的,对于一个Android应用的用户界面来说,ViewGroup作为容器盛装界面中的控件,它可以包含普通的View控件,也可以包 ...

  4. android10桌面布局好看,让你的Android手机界面布局更好看

    机友是否觉得你的Android界面布局不够好看?想换一种手机界面?也许很多朋友都能够很简单Android界面美化,但是太深入的操作就不会了,那么下面我为大家介绍另一种风格的界面,使你的手机界面变的更加 ...

  5. 《Android移动应用基础教程》(Android Studio)(第二版)黑马教程 课后题答案第2章 Android常见界面布局

    一.填空题 1.ViewGroup 2.LinearLayout 3.TableRow 4.RelativeLayout LinearLayout 5.iInt 二.判断题 1.√ 2.× 3.√ 4 ...

  6. 第2章 Android常见界面布局

    课后习题 1. 列举Android中的常用布局,并简述他们各自的特点 Android中有五种常用布局,分别为RelativeLayout(相对布局).LinearLayout(线性布局).FrameL ...

  7. (2)Android常见界面布局

    文章目录 2.1 View控件 2.2 界面布局编写方式 XML中编写 Java中编写 2.3 界面布局的通用属性 android:id android:layout_width&&a ...

  8. Android 常用界面布局

    android-studio-2020.3.1.25-windows安装包 链接:https://pan.baidu.com/s/19SgimjO3OJmkA2mHjfrXvw 提取码:pt0d 实验 ...

  9. android 自定义课程表,Android课程表界面布局实现代码

    前言 Android课程表布局实现 我是个菜鸟,文章供参考 示例 图1: 图2: 布局分析 该界面主要可分为三部分: 1.显示年份及周数部分 2.显示周一到周日 3.课程显示部分 实现步骤 1.首先整 ...

  10. Android复杂界面布局解决方案

    前言     最近接近年关,公司项目没什么事情做.闲暇之余的时间笼统的做了一个app的框架(虽然我不知道这样算不算是框架).      我们知道,很多App的界面是非常复杂的,如果按照常规的方法去写l ...

最新文章

  1. 成功解决WARNING: You do not appear to have an NVIDIA GPU supported by the 430.34 NVIDIA Linux graph
  2. 双赛道近四百万奖金,2021全国人工智能大赛来了!
  3. 做过这个NLP项目的面试通过率高达 90%!!
  4. Spring IOC实现原理
  5. shell脚本详解(十二)——Here Document免交互及Expect自动化交互
  6. 并发执行变成串行_大白话Java并发面试问题之Java 8如何优化CAS性能?
  7. 【jzoj】2018.2.1 NOIP普及组——D组模拟赛
  8. 【单调栈】向右看齐(luogu 2947)
  9. antd 表格树如何展开_如何分析工作,找出规律,使用excel大幅度提高工作效率...
  10. javaWeb项目部署至tomcat下classes文件夹没有内容
  11. ECCV2020 Oral | 图像修复之再思考
  12. vue 编辑弹框,编辑页面,列表数据也会跟着变
  13. Vivado入门创建工程之----流水灯
  14. Linux I2C总线驱动调试之i2c-tools工具使用
  15. java wsdl文件生成代码_如何用myeclipse将wsdl文件生成java代码
  16. 2022前端HTML5面试题
  17. (纪中)3505. 积木【DP】
  18. SDN网络下有哪些SDN交换机选择?
  19. 怎样理解大数据概念?大数据有什么用处?
  20. tikz 折线 箭头_[LaTeX 绘图] tikz 绘制流程图,概述和两个示例

热门文章

  1. 凸二次规划的解法(x取值具有上确界)
  2. 软考-中级软件设计师电子资料合集
  3. SAP常用TCODE
  4. python解析器下载_pak文件解析-pak文件解析工具下载Python版-西西软件下载
  5. 深入浅出看懂AlphaGo Zero (文章最后有原AlphaGo Zero论文地址)
  6. 计算机网络智能化在铁路通信的发展,浅谈新技术在铁路通信中的应用
  7. 基于模糊PID控制的光伏系统最大功率点跟踪的研究
  8. 计算机基础知识集体备课,计算机基础知识集体备课.doc
  9. 数学建模之概率模型详解
  10. python定时重启程序