Android计算器LinearLayout实现布局
计算器布局的实现有好多方法,先用LinearLayout实现一次吧,以后可能还会用其他布局
先上最终结果
实现方式是利用LinearLayout的layout_weight属性,实现按钮的均匀分布
由于layout_weight属性的特殊性,
当外层LinearLayout为垂直排布的时候,内部控件的layout_height要设为0dp,layout_width设为match_parent
当外层LinearLayout为水平排布的时候,内部控件的layout_width要设为0dp,layout_height设为match_parent
否则,最后的结果可能并不会像你想象得那样
这是一个简单到不能再简单的计算器了,只有加减乘除,
布局可以分为
第一行EditText layout_weight="1"
第二行,第三行,第四行分别有四个按钮 layout_weight="1"
第五行和第六行因为最后有一个占两行的等号
所以再嵌套一层
最外层的LinearLayout layout_weight=2
里边嵌套四个垂直排列的LinearLayout
最外层当然是一个
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"></LinearLayout>
第一个控件是一个EditText
设置从右到左编辑,点击不会唤醒键盘
<EditTextandroid:id="@+id/editText"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:gravity="end|bottom"android:editable="false"/>
剩下的就是LinearLayout里套Button 或者LinearLayout里套LinearLayout里再套Button了
全部代码如下,id起的不是很规范,英语渣(的不能再渣)
<?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"><EditTextandroid:id="@+id/editText"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:gravity="end|bottom"android:editable="false"/><!--第一行按钮c,÷,×,del--><LinearLayoutandroid:layout_weight="1"android:layout_width="match_parent"android:layout_height="0dp"android:orientation="horizontal"><Buttonandroid:id="@+id/bt_clear"android:layout_weight="1"android:layout_width="0dp"android:layout_height="match_parent"android:text="C"/><Buttonandroid:id="@+id/bt_divide"android:layout_weight="1"android:layout_width="0dp"android:layout_height="match_parent"android:text="÷"/><Buttonandroid:id="@+id/bt_multiply"android:layout_weight="1"android:layout_width="0dp"android:layout_height="match_parent"android:text="×"/><Buttonandroid:id="@+id/bt_delete"android:layout_weight="1"android:layout_width="0dp"android:layout_height="match_parent"android:text="del"/></LinearLayout><!--第二行按钮7,8,9,- --><LinearLayoutandroid:layout_weight="1"android:layout_width="match_parent"android:layout_height="0dp"android:orientation="horizontal"><Buttonandroid:id="@+id/num7"android:layout_weight="1"android:layout_width="0dp"android:layout_height="match_parent"android:text="7"/><Buttonandroid:id="@+id/num8"android:layout_weight="1"android:layout_width="0dp"android:layout_height="match_parent"android:text="8"/><Buttonandroid:id="@+id/num9"android:layout_weight="1"android:layout_width="0dp"android:layout_height="match_parent"android:text="9"/><Buttonandroid:id="@+id/sub"android:layout_weight="1"android:layout_width="0dp"android:layout_height="match_parent"android:text="-"/></LinearLayout><!-- 第三行按钮 4,5,6,+ --><LinearLayoutandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:orientation="horizontal"><Buttonandroid:id="@+id/num4"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:text="4" /><Buttonandroid:id="@+id/num5"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:text="5" /><Buttonandroid:id="@+id/num6"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:text="6" /><Buttonandroid:id="@+id/plus"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:text="+" /></LinearLayout><!-- 第四,五行按钮 --><LinearLayoutandroid:layout_weight="2"android:layout_width="match_parent"android:layout_height="0dp"android:orientation="horizontal"><!-- 第一列按钮 1,% --><LinearLayoutandroid:layout_weight="1"android:layout_width="0dp"android:layout_height="match_parent"android:orientation="vertical"><Buttonandroid:id="@+id/num1"android:layout_weight="1"android:layout_width="match_parent"android:layout_height="0dp"android:text="1"/><Buttonandroid:id="@+id/bt010"android:layout_weight="1"android:layout_width="match_parent"android:layout_height="0dp"android:text="%"/></LinearLayout><!-- 第二列按钮 2 , 0 --><LinearLayoutandroid:layout_weight="1"android:layout_width="0dp"android:layout_height="match_parent"android:orientation="vertical"><Buttonandroid:id="@+id/num2"android:layout_weight="1"android:layout_width="match_parent"android:layout_height="0dp"android:text="2"/><Buttonandroid:id="@+id/num0"android:layout_weight="1"android:layout_width="match_parent"android:layout_height="0dp"android:text="0"/></LinearLayout><!-- 第三列按钮 3,. --><LinearLayoutandroid:layout_weight="1"android:layout_width="0dp"android:layout_height="match_parent"android:orientation="vertical"><Buttonandroid:id="@+id/num3"android:layout_weight="1"android:layout_width="match_parent"android:layout_height="0dp"android:text="3"/><Buttonandroid:id="@+id/btpdot"android:layout_weight="1"android:layout_width="match_parent"android:layout_height="0dp"android:text="."/></LinearLayout><!-- 第四列按钮 = --><LinearLayoutandroid:layout_weight="1"android:layout_width="0dp"android:layout_height="match_parent"android:orientation="vertical"><Buttonandroid:id="@+id/bted"android:layout_weight="1"android:layout_width="match_parent"android:layout_height="0dp"android:text="="/></LinearLayout></LinearLayout></LinearLayout>
Android计算器LinearLayout实现布局相关推荐
- Android之LinearLayout线性布局
1.相关术语解释 orientation 子控件的排列方式,horizontal(水平)和vertical(垂直默认)两种方式! gravity 子控件的对齐方式,多个组合 layout_gravit ...
- Android精通:View与ViewGroup,LinearLayout线性布局,RelativeLayout相对布局,ListView列表组件...
UI的描述 对于Android应用程序中,所有用户界面元素都是由View和ViewGroup对象构建的.View是绘制在屏幕上能与用户进行交互的一个对象.而对于ViewGroup来说,则是一个用于存放 ...
- Android零基础入门第25节:最简单最常用的LinearLayout线性布局
原文:Android零基础入门第25节:最简单最常用的LinearLayout线性布局 良好的布局设计对于UI界面至关重要,在前面也简单介绍过,目前Android中的布局主要有6种,创建的布局文件默认 ...
- 精通android布局,Android精通:View与ViewGroup,LinearLayout线性布局,RelativeLayout相对布局,ListView列表组件...
标题图 UI的描述 对于Android应用程序中,所有用户界面元素都是由View和ViewGroup对象构建的.View是绘制在屏幕上能与用户进行交互的一个对象.而对于ViewGroup来说,则是一个 ...
- android 前台服务自定义布局不显示_Android自定义LinearLayout布局显示不完整的解决方法...
发现问题 原需求,在一个伸缩列表中,自定义LinearLayout继承LinearLayout动态添加布局. 然而实现的时候:一共遍历了30条数据,却只显示了一条 断点查看代码:遍历addView() ...
- Android 应用开发(36)---LinearLayout(线性布局)
LinearLayout(线性布局) 本节开始讲Android中的布局,Android中有六大布局,分别是: LinearLayout(线性布局),RelativeLayout(相对布局),Table ...
- android计算器布局(TableLayout)
用TableLayout写的android计算器布局代码: activity_main.xml文件: <GridLayout xmlns:android="http://schemas ...
- android线性布局设置控件固定在底部,Android UI组件LinearLayout线性布局详解
LinearLayout 线性布局,该布局的继承关系: 1. 什么是线性布局 通俗的说感觉起来和线有关,参照线的特点,有么是横向的,要么是竖向的. LinearLayout是线性布局控件,它包含的子控 ...
- Android LinearLayout 各布局属性总结
LinearLayout是开发中使用率很高的控件,大部分开发人员感觉应该是很熟悉了,不过LinearLayout还有如下几个需要注意的地方,深刻了解以下几点可以让我们更加全面地认识LinearLayo ...
- 【Android 】零基础到飞升 | LinearLayout(线性布局)
2.2.1 LinearLayout(线性布局) 本节引言 本节开始讲Android中的布局,Android中有六大布局,分别是: LinearLayout(线性布局),RelativeLayout( ...
最新文章
- pandas改变dataframe的列的顺序、改变数据列的排列次序
- Ubuntu下Postgres安装与配置
- mongodb更新某个字段_直播 | MongoDB开源数据库的云上之路
- python函数“转移”
- 一个中年程序员遇到突发情况的一些胡言乱语
- Java多线程编程 — 锁优化
- 海量数据处理相关算法及数据结构【转】
- nginx负载均衡原理(理解篇)
- Design Tradeoffs for SSD Performance
- oracle job定时报错,Oracle定时任务Job笔记
- Starting Programe
- java模拟器.apk_java游戏模拟器安卓版下载-java模拟器apk下载 v2.2.0 安卓版-IT猫扑网...
- C语言使用栈和队列实现停车场管理
- 【小型系统】简单的刷票系统(突破IP限制进行投票)
- PHP网页输出xml数据
- matlab 数学形态学,数学形态学matlab程序
- docker 安装linux镜像制作,制作ubuntu完整版docker镜像
- VisualStudio2012的序列号
- 北斗/GPS如何实现定位
- uniapp学习教程
热门文章
- 九阴真经 服务器列表文件,九阴真经合服_九阴真经数据互通_九阴真经公告_快吧游戏...
- 开机出现recovering journal解决办法
- 瞎琢磨先生のJava笔记之读取SpringBoot配置文件的几种方式
- HTB-Apocalyst
- 【自勉-向后端架构师/音视频架构师出发】我的目标是星辰大海(4月)
- influxdb java api使用_Influxdb入门使用
- CSDN博客排版格式入门
- Redhat8.0最小化安装没有tab补全命令
- 2020-01-02 bibtex生成的参考文献如何转为thebibliography
- java pack unpack_解释一下pack和unpack