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之前,我们通常说 ...
随机推荐
- linux-3.14.13 看到mpls gso支持
在net/Kconfig source "net/mpls/Kconfig"
- 初涉Linux ----------> Ubuntu15.04的安装与美化
“你玩 Linux 吗?” “什么 Linux ?” “你连 Linux 都不知道?还说是学计算机的呢...” “干嘛要用 Linux 啊?windows多好,Linux?没兴趣” 一. 前言 ...
- perl多线程理解
Thread:在使用多线程处理比较大的数据量的扫描,遇到读写文件可能死锁的问题. Perl 线程的生命周期 1.使用 threads 包的 create() 方法: use threads; sub ...
- 使用jlink直接烧norflash或者nandflash不借助uboot的猜想
由于喜欢折腾,我是在linux下使用jlink的,既然JLinkExe可以进行内存读写操作,loadbin等操作,并且通过指定命令文件支持批量指令输入,那么首先jlink是可以直接访问内部存储器的,包 ...
- Codeforces 461B. Appleman and Tree[树形DP 方案数]
B. Appleman and Tree time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- 转:Eclipse SVN插件比较 Subclipse vs Subversive
结论:还是用久经考验的 Subclipse http://www.wkii.org/eclipse-svn-plugins-subclipse-vs-subversive.html
- AFN 处理网络哪些事(轻松掌握AFN网络顶级框架)
AFN 一.什么是AFN 全称是AFNetworking,是对NSURLConnection的一层封装 虽然运行效率没有ASI高,但是使用比ASI简单 在iOS开发中,使用比较广泛 AFN的githu ...
- UIDate(时间)
1.时间与时间的比较 ["时间1" timeIntervalSinceDate:"时间2"] >= 0;
- 【点滴积累,厚积薄发】windows schedule task中.exe程序的路径问题等问题总结
1.在发布ReportMgmt的Job时遇到一个路径问题,代码如下: doc.Load(@"Configuration\Business\business.config"); ...
- Linux 信号详解五(信号阻塞,信号未决)
信号在内核中的表示 执行信号的处理动作成为信号递达(Delivery),信号从产生到递达之间的状态称为信号未决(Pending).进程可以选择阻塞(Block)某个信号. 被阻塞的信号产生时将保持在未 ...