android开发------编写用户界面之线性布局(补充知识)
在前面的文章中 http://www.cnblogs.com/ai-developers/p/android_linearlayout.html
我们看到了布局中有这样一个属性:
layout_weight="1"
它的作用是什么。
我们先来做一个假设:有一个界面,要求元素在垂直方向上所占的空间一样,你会怎样做呢?
有人会说:将元素的属性layout_height设置相同的值就可以了啊。确实这样是可以的。
但是如果我有一个要求:这些元素所占的总空间要刚好匹配Activity的大小,不能有溢出。
那你会不会用尺子先量一下Activity的高度,再将值平均分配给各个元素?
当然这样做很傻。只是开个玩笑。
先来看一下下面代码的运行效果
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"> <Button
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_gravity="center"
android:text="发送"/> <Button
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_gravity="center"
android:text="发送"/> <Button
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_gravity="center"
android:text="发送"/> <Button
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_gravity="center"
android:text="发送"/>
<Button
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_gravity="center"
android:text="发送"/>
</LinearLayout>
这里我们定义了5个按钮,但是有两个溢出,已经看不见了

我们改进一下代码,为每个元素添加一个layout_weight属性
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"> <Button
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_gravity="center"
android:layout_weight="1"
android:text="发送"/> <Button
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_gravity="center"
android:layout_weight="1"
android:text="发送"/> <Button
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_gravity="center"
android:layout_weight="1"
android:text="发送"/> <Button
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_gravity="center"
android:layout_weight="1"
android:text="发送"/>
<Button
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_gravity="center"
android:layout_weight="1"
android:text="发送"/> </LinearLayout>
再运行一下试试,现在程序要求达到了,但是我们发现每个按钮的高度都改变了,layout_height属性不起作用了

那我们可以删掉layout_height属性吗。答案是否定的,程序会崩溃。下面是logCat的错误信息:

我们的布局文件必须提供layout_width和layout_height属性
layout_weight属性的工作原理:
android开发------编写用户界面之线性布局(补充知识)的更多相关文章
- android开发------编写用户界面之线性布局
一个好的应用程序离不开人性化的用户界面.在学习其他东西之前.理应先学习编写程序的布局(外观) 今天,我们就来学习android的UI布局----LinearLayout. LinearLayout,即 ...
- android开发------编写用户界面之相对布局
今天要说的是RelativeLayout.RelativeLayout相对于LinearLayout的主要不同点在于它需要一个参照物. 我们先来看一下官方对这个布局的解释: RelativeLayou ...
- Android开发之详解五大布局
http://bbs.chinaunix.net/thread-3654213-1-1.html 为了适应各式各样的界面风格,Android系统提供了5种布局,这5种布局分别是: LinearLayo ...
- Android开发之玩转FlexboxLayout布局
在这之前,我曾认真的研究过鸿洋大神的Android 自定义ViewGroup 实战篇 -> 实现FlowLayout,按照大神的思路写出了一个流式布局,所有的东西都是难者不会会者不难,当自己能自 ...
- android 开发 RecyclerView 横排列列表布局
1.写一个一竖的自定义布局: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xml ...
- Android之UI编程(一):线性布局
package com.example.fk_layout; import android.app.Activity; import android.os.Bundle; public class L ...
- Android 12(S) 图形显示系统 - Surface 一点补充知识(十二)
必读: Android 12(S) 图形显示系统 - 开篇 一.前言 因为个人工作主要是Android多媒体播放的内容,在工作中查看源码或设计程序经常会遇到调用API: static inline i ...
- android开发4:Android布局管理器1(线性布局,相对布局RelativeLayout-案例)
控件类概述 View 可视化控件的基类 属性名称 对应方法 描述 android:background setBackgroundResource(int) 设置背景 android:clickabl ...
- Android开发自学笔记(Android Studio)—4.1布局组件
一.引言 Android的界面是有布局和组件协同完成的,布局好比是建筑里的框架,而组件则相当于建筑里的砖瓦.组件按照布局的要求依次排列,就组成了用户所看见的界面.在Android4.0之前,我们通常说 ...
随机推荐
- vs2008环境nmake编译 apache 2.2.29 openssl 1.0.1g mod_ssl 不知道如何生成“"..\..\srclib\openssl\inc32\openssl\store.h"”
问题: vs2008环境nmake编译 apache 2.2.29 openssl 1.0.1g mod_ssl 不知道如何生成“"..\..\srclib\openssl\inc32\op ...
- monkeyrunner之夜神模拟器的安装与使用(二)
在上一篇文章-安卓开发环境搭建中,我们创建并启动了eclipse自带的安卓模拟器,该模拟器不仅启动慢,而且在使用过程中的反应速度也是出奇的差,经常出现卡机现象.为了解决这种现象,因此,我们又寻找到了更 ...
- Unity4.3 bug GetChild顺序错乱
历史原因,目前有个项目还在使用unity4.3版本,比较过不同Unity版本,发现unity4.3的 transform.GetChild 获取的child顺序并不是想要的. 测试代码 using U ...
- OAuth2授权原理
最近在做第三方接入的,初步定下使用OAuth2协议,花了些时间对OAuth2的授权方式做了些了解. 我还记得一两年前,跟一位同事聊起互联网时,当时我说过一个想法: 目前不少较为稀有的资源,很多都是论坛 ...
- jsp的九大内置对象和四大作用域
定义:可以不加声明就在JSP页面脚本(Java程序片和Java表达式)中使用的成员变量? JSP共有以下9种基本内置组件(可与ASP的6种内部组件相对应):? 1.request对象(作用域)? 客户 ...
- java 22 - 18 多线程之 线程的状态转换、线程组
线程的状态转换图解:图片 线程的线程组: 线程组: 把多个线程组合到一起. 它可以对一批线程进行分类管理,Java允许程序直接对线程组进行控制. 首先创建一个Runnable的实现类 publi ...
- 转: Git远程操作详解 (阮一峰)
说明: 通过这个说明终于把远程操作给搞明白了. http://www.ruanyifeng.com/blog/2014/06/git_remote.html
- uva131 The Psychic Poker Player
The Psychic Poker Player Time Limit: 3000MS 64bit IO Format: %lld & %llu Description In 5-ca ...
- iOS UITableView 分割线从零开始
第一种(不自己画线): 代码如下 // tableView的分割线从零开始 -(void)viewDidLayoutSubviews { if ([self.tableView respondsToS ...
- Apache Rewrite 拟静态配置
1.mod_rewrite 简介和配置 Rewirte主要的功能就是实现URL的跳转和隐藏真实地址,基于Perl语言的正则表达式规范.平时帮助我们实现拟静态,拟目录,域名跳转,防止盗链等 2.mod_ ...