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

<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. tensorflow-训练(train)/测试(test)

    一个TFRecords 文件为一个字符串序列.这种格式并非随机获取,它比较适合大规模的数据流,而不太适合需要快速分区或其他非序列获取方式. 操作组 操作 Training Optimizers,Gra ...

  2. EntityFramework 系列:实体类配置-根据依赖配置关系和关联

    EF实体类的配置可以使用数据注释或Fluent API两种方式配置,Fluent API配置的关键在于搞清实体类的依赖关系,按此方法配置,快速高效合理.为了方便理解,我们使用简化的实体A和B以及A.B ...

  3. windows下安装mysql8.0压缩版

         下面总结下安装过程: 首先解压下载好的压缩版本. 将解压后mysql的bin文件目录配置系统环境path变量中 使用cmd打开命令窗口,输入mysqld  --initialize命令初始化 ...

  4. MongoDB CPU使用较高,如何排查?

    前言 首先,我们简单梳理一下,CPU 在什么情况下才算负载较高?负载查看是通过"uptime"命令查看.大家都知道,命令显示的结果分别表示1分钟.5分钟.15分钟的负载情况,这点就 ...

  5. P1508 Likecloud-吃、吃、吃 DP

    题目背景 问世间,青春期为何物? 答曰:“甲亢,甲亢,再甲亢:挨饿,挨饿,再挨饿!” 题目描述 正处在某一特定时期之中的李大水牛由于消化系统比较发达,最近一直处在饥饿的状态中.某日上课,正当他饿得头昏 ...

  6. getImplementationVersion-获取版本号

    在工作中会遇到这个方法的使用,就记录一下. 一:getImplementationVersion 1.  方法 java.lang.Package.getImplementationVersion() ...

  7. 深入理解Git - 一切皆commit

    在对 git 有了基本理解和知道常规操作之后,如何对 git 的使用有进一步的理解? 一切皆 commit 或许是个不错的理解思路. 本文将从『一切皆 commit 』的角度,通过 git 中常见的名 ...

  8. Java 多线程 - synchronize 关键字

    目录 Java 多线程 - synchronize 关键字 Java 多线程 - synchronize 关键字 学习自 http://cmsblogs.com/?p=2071 https://www ...

  9. 暴力探测蓝牙设备工具redfang

    暴力探测蓝牙设备工具redfang   根据是否可以被扫描到,蓝牙设备具有可见(Disoverable)和不可见(non discoverable)两种模式.为了扫描不可见蓝牙设备,Kali Linu ...

  10. Python学习——Python基本数据类型(一)

    python3 基本数据类型 Python3 中的变量不需要声明.每个变量在使用前都必须赋值,变量赋值以后该变量才会被创建. Python3 中有6个标准的数据类型:Number(数字);字符串(St ...