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之前,我们通常说 ...
随机推荐
- if_nametoindex可以检查网卡名称是否有效
NAME if_nametoindex, if_indextoname, if_nameindex, if_freenameindex - convert interface index to nam ...
- 关于TCP连接建立与终止那点事
0. 前言 最近在处理公司遗留项目的时候发现自己对TCP协议一点都不懂,所以补了点关于TCP连接的建立和终止的内容,这里简单写下自己了解的部分,省略了报文序号确认序号这些无关的字段,主要讨论TCP状态 ...
- Linux压缩指令
1.windows系统和Linux系统的压缩文件只有zip格式的是通用的 2.windows系统可以解压几乎所有的Linux压缩格式 3.Linux压缩指令 gzip 指令 格式: gzip 压缩的文 ...
- plain framework 1 参考手册 入门指引之 许可协议
许可 介绍 Apache2 License 介绍 使用非常友好的阿帕奇2许可协议,你可以应用到你想用的任何应用上,如需修改源码,请保留原作者信息. Apache2 License Apache Lic ...
- zookeeper Eclipse 开发环境搭建及简单示例
一,下载Zookeeper安装包 从官方网站下载稳定版安装包后,解压. 其中ZK_HOME 为:D:\Program Files\zookeeper-3.4.9 二,启动Zookeeper Serve ...
- 12个JQuery小贴士
返回顶部 使用JQuery的 animate 和 scrollTop 方法可以创建简单地返回顶部的动画: // Back to top $('a.top').click(function (e) { ...
- js+JQuery实现返回顶部功能
很多网站上都有返回顶部的效果,本文阐述如何使用jquery实现返回顶部按钮. 首先需要在顶部添加如下html元素: <p id="back-to-top"><a ...
- jQuery offset,position,offsetParent,scrollLeft,scrollTop html控件定位 css position
定位应用:点击一个按钮,然后在按钮的右边弹出一个提示框 1,提示框相对于屏幕进行定位,那么使用offset来取得当前按钮相对于body的top和left,然后通过$('body').prepend(t ...
- redis 学习笔记(4)-HA高可用方案Sentinel配置
上一节中介绍了master-slave模式,在最小配置:master.slave各一个节点的情况下,不管是master还是slave down掉一个,“完整的”读/写功能都将受影响,这在生产环境中显然 ...
- 2016古装动作喜剧《笨贼别跑》HD720P.国语中字
导演: 雷金克编剧: 郭卫鹏 / 李诗怡 / 马强主演: 彭波 / 李添诺 / 董向荣 / 韩丰 / 董怡君类型: 喜剧 / 动作 / 古装制片国家/地区: 中国大陆语言: 汉语普通话上映日期: 20 ...