所谓的嵌套布局就是在一个文件里嵌套多个布局文件

<span style="font-size:18px;">  <LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
……
……
……
</FrameLayout > </LinearLayout></span>

接下来使用嵌套布局来实现一个简单计算器的UI效果

以下是基本构思图

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzYxNjk3Ng==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

使用Xml布局文件设置计算器效果

<span style="font-size:18px;"><?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" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="3"
android:orientation="vertical" > <TextView
android:id="@+id/text2"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_marginTop="0dp"
android:layout_weight="1"
android:padding="0dp" /> <TextView
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_marginTop="0dp"
android:layout_weight="2"
android:padding="0dp" />
</LinearLayout> <TableLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="vertical" > <TableRow
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="0.2" > <Button
android:id="@+id/left_bracket_button"
android:layout_height="fill_parent"
android:layout_marginBottom="1dp"
android:layout_marginLeft="1dp"
android:layout_weight="0.25"
android:background="#333333"
android:text="("
android:textColor="@android:color/white"
android:textSize="25sp" /> <Button
android:id="@+id/right_bracket_button"
android:layout_height="fill_parent"
android:layout_marginBottom="1dp"
android:layout_marginLeft="1dp"
android:layout_weight="0.25"
android:background="#333333"
android:text=")"
android:textColor="@android:color/white"
android:textSize="25sp" /> <Button
android:id="@+id/divisor_button"
android:layout_height="fill_parent"
android:layout_marginBottom="1dp"
android:layout_marginLeft="1dp"
android:layout_weight="0.25"
android:background="#333333"
android:text="÷"
android:textColor="@android:color/white"
android:textSize="25sp" /> <Button
android:id="@+id/backspace_button"
android:layout_height="fill_parent"
android:layout_marginBottom="1dp"
android:layout_marginLeft="1dp"
android:layout_weight="0.25"
android:background="#ffa500"
android:text="C"
android:textSize="25sp" />
</TableRow> <TableRow
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="0.2" >
<Button
android:id="@+id/seven_button"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_weight="0.25"
android:layout_marginLeft="1dp"
android:layout_marginBottom="1dp"
android:textSize="25sp"
android:background="@android:color/darker_gray"
android:text="7" /> <Button
android:id="@+id/eight_button"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_weight="0.25"
android:layout_marginLeft="1dp"
android:layout_marginBottom="1dp"
android:textSize="25sp"
android:background="@android:color/darker_gray"
android:text="8" /> <Button
android:id="@+id/nine_button"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_weight="0.25"
android:layout_marginLeft="1dp"
android:layout_marginBottom="1dp"
android:textSize="25sp"
android:background="@android:color/darker_gray"
android:text="9" /> <Button
android:id="@+id/product_button"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_weight="0.25"
android:layout_marginLeft="1dp"
android:layout_marginBottom="1dp"
android:background="#333333"
android:textSize="25sp"
android:textColor="@android:color/white"
android:text="×" />
</TableRow> <TableRow
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="0.2" >
<Button
android:id="@+id/four_button"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_weight="0.25"
android:layout_marginLeft="1dp"
android:layout_marginBottom="1dp"
android:textSize="25sp"
android:background="@android:color/darker_gray"
android:text="4" /> <Button
android:id="@+id/five_button"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_weight="0.25"
android:layout_marginLeft="1dp"
android:layout_marginBottom="1dp"
android:textSize="25sp"
android:background="@android:color/darker_gray"
android:text="5" /> <Button
android:id="@+id/six_button"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_weight="0.25"
android:layout_marginLeft="1dp"
android:layout_marginBottom="1dp"
android:textSize="25sp"
android:background="@android:color/darker_gray"
android:text="6" /> <Button
android:id="@+id/minus_button"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_weight="0.25"
android:layout_marginLeft="1dp"
android:layout_marginBottom="1dp"
android:background="#333333"
android:textSize="25sp"
android:textColor="@android:color/white"
android:text="-" />
</TableRow> <TableRow
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="0.2" >
<Button
android:id="@+id/one_button"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_weight="0.25"
android:layout_marginLeft="1dp"
android:layout_marginBottom="1dp"
android:textSize="25sp"
android:background="@android:color/darker_gray"
android:text="1" /> <Button
android:id="@+id/two_button"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_weight="0.25"
android:layout_marginLeft="1dp"
android:layout_marginBottom="1dp"
android:textSize="25sp"
android:background="@android:color/darker_gray"
android:text="2" /> <Button
android:id="@+id/three_button"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_weight="0.25"
android:layout_marginLeft="1dp"
android:layout_marginBottom="1dp"
android:textSize="25sp"
android:background="@android:color/darker_gray"
android:text="3" /> <Button
android:id="@+id/plus_button"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_weight="0.25"
android:layout_marginLeft="1dp"
android:layout_marginBottom="1dp"
android:background="#333333"
android:textSize="25sp"
android:textColor="@android:color/white"
android:text="+" />
</TableRow> <TableRow
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="0.2" >
<Button
android:id="@+id/point_button"
android:layout_height="fill_parent"
android:layout_width="wrap_content"
android:layout_weight="0.5"
android:layout_marginLeft="1dp"
android:layout_marginBottom="1dp"
android:textSize="40sp"
android:background="@android:color/darker_gray"
android:text="." /> <Button
android:id="@+id/zero_button"
android:layout_height="fill_parent"
android:layout_width="wrap_content"
android:layout_weight="0.5"
android:layout_marginLeft="1dp"
android:layout_marginBottom="1dp"
android:textSize="25sp"
android:background="@android:color/darker_gray"
android:text="0" /> <Button
android:id="@+id/equal_button"
android:layout_height="fill_parent"
android:layout_width="wrap_content"
android:layout_weight="3.1"
android:layout_marginLeft="1dp"
android:layout_marginBottom="1dp"
android:textSize="25sp"
android:background="#00CC00"
android:text="=" />
</TableRow>
</TableLayout> </LinearLayout></span>

以下是模拟器效果图

上面的代码中重复用到了

<span style="font-size:18px;"> android:layout_weight=""</span>

要想设置好一个布局就必须了解权重的概念android:layout_weight的真实含义

下节预报:

滚动视图ScrollView

从零開始学android&lt;使用嵌套布局实现计算器界面.十七.&gt;的更多相关文章

  1. 从零開始学android&lt;RelativeLayout相对布局.十六.&gt;

    相对布局管理器指的是參考某一其它控件进行摆放,能够通过控制,将组件摆放在一个指定參考组件的上.下.左.右等位置,这些能够直接通过各个组件提供的属性完毕. 以下介绍一下各个方法的基本使用 No. 属性名 ...

  2. 从零開始学android&lt;数据存储(1)SharedPreferences属性文件.三十五.&gt;

    在android中有五种保存数据的方法.各自是: Shared Preferences Store private primitive data in key-value pairs. 相应属性的键值 ...

  3. 第13章、布局Layouts之RelativeLayout相对布局(从零開始学Android)

    RelativeLayout相对布局 RelativeLayout是一种相对布局,控件的位置是依照相对位置来计算的,后一个控件在什么位置依赖于前一个控件的基本位置,是布局最经常使用,也是最灵活的一种布 ...

  4. 从零開始学android&lt;SeekBar滑动组件.二十二.&gt;

    拖动条能够由用户自己进行手工的调节,比如:当用户须要调整播放器音量或者是电影的播放进度时都会使用到拖动条,SeekBar类的定义结构例如以下所看到的: java.lang.Object    ↳ an ...

  5. 从零開始学android&lt;mediaplayer自带播放器(视频播放).四十九.&gt;

    MediaPlayer除了能够对音频播放之外,也能够对视频进行播放,可是假设要播放视频仅仅依靠MediaPlayer还是不够的.还须要编写一个能够用于视频显示的空间,而这块显示空间要求能够高速的进行G ...

  6. 从零開始学android&lt;TabHost标签组件.二十九.&gt;

    TabHost主要特点是能够在一个窗体中显示多组标签栏的内容,在Android系统之中每一个标签栏就称为一个Tab.而包括这多个标签栏的容器就将其称为TabHost.TabHost类的继承结构例如以下 ...

  7. 从零開始开发Android版2048 (一)初始化界面

    自学Android一个月多了,一直在工作之余零零散散地看一些东西.感觉经常使用的东西都有些了解了,可是一開始写代码总会出各种奇葩的问题.感觉还是代码写得太少.这样继续杂乱地学习下去进度也太慢了,并且学 ...

  8. 从零開始学android&lt;Menu菜单组件.三十.&gt;

    在Android系统之中.菜单一共同拥有三类:选项菜单(OptionsMenu).上下文菜单(ContextMenu)和子菜单(SubMenu). 今天我们就用几个样例来分别介绍下菜单的使用 acti ...

  9. 从零開始学android&lt;ImageSwitcher图片切换组件.二十六.&gt;

    ImageSwitcher组件的主要功能是完毕图片的切换显示,比如用户在进行图片浏览的时候.能够通过button点击一张张的切换显示的图片,并且使用ImageSwitcher组件在每次切换的时候也能够 ...

随机推荐

  1. 【linux】监控磁盘情况并自动删除备份文件

    背景:我有一个备份目录/home/kzy/bakup,会每天备份一些信息.随着日子一天天的过去,这个文件夹越来越大,终于把磁盘撑满了..... 需求:当磁盘占有率超过80%时自动删除该文件夹下最老的3 ...

  2. Git安装及密钥的生成

    1.下载Git软件:http://msysgit.github.io/ 2.安装git软件(很简单).安装成功后,在[开始]->[程序]->[git],下就会看见Git Bash和Git ...

  3. JS易错知识点

    JAVASCRIPT易错知识点整理 前言 本文是学习JavaScript过程中收集与整理的一些易错知识点,将分别从变量作用域,类型比较,this指向,函数参数,闭包问题及对象拷贝与赋值这6个方面进行由 ...

  4. 【LOJ】#2265. 「CTSC2017」最长上升子序列

    题解 点了一个新技能叫杨表(事实上集训的时候听过,但是一直不会 这道题就是让我们找到k个不上升子序列,要求长度加和最大 我们用杨表去维护,但是由于杨表的行数可能是n的,复杂度会炸 我们只维护前\(\s ...

  5. Codeforces Round #475 (Div. 2) C - Alternating Sum

    等比数列求和一定要分类讨论!!!!!!!!!!!! #include<bits/stdc++.h> #define LL long long #define fi first #defin ...

  6. 牛客练习赛16 E - 求值

    题目大意: 链接:https://www.nowcoder.com/acm/contest/84/E 给定n个数字a1, a2, ..., an. 定义f(l, r) = al | al+1| ... ...

  7. Mysql 查看用户

    一.查看用户和允许用户登录的地址权限. use mysql select host,user from user; 二.查看用户和所有用户权限 select * from user \G

  8. 【笔试题】在 Java 中,如何跳出当前的多重嵌套循环?

    笔试题 在 Java 中,如何跳出当前的多重嵌套循环? public class Demo { public static void main(String[] args) { System.out. ...

  9. CSS------li中的宽和高无法修改问题

    如图: 代码:(需要将display属性值设置为inline-block) <ul style="margin-top:50px"> <li style=&quo ...

  10. ThinkPHP V5.0 正式版发布

    ThinkPHP5.0版本是一个颠覆和重构版本,官方团队历时十月,倾注了大量的时间和精力,采用全新的架构思想,引入了更多的PHP新特性,优化了核心,减少了依赖,实现了真正的惰性加载,支持compose ...