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之前,我们通常说 ...
随机推荐
- PCI在linux系统中注册与注销示例
1. pci_driver结构struct pci_driver { struct list_head node; const char *name; const struct pc ...
- SQLITE配置环境变量
SQLITE配置环境变量和Java的配置其实是一样的,不过好像每个人配的时候不完全相同, 有的机器可以,另外一些机器同样的方法却行不通,总之思路是一样的多试几种,总有合适的参数 下面是我在配置时截 ...
- proteus 运行出错,用户名不可使用中文!
仿真的时候提示如图提示 cannot open ’c\user\小名\AppData\local\temp\LISA0089.sdf’ 系统用户名不能是中文! 解决办法:重新建立个账户,记得用英文命名 ...
- Jquery 的遍历,祖先、后代、同胞以及其过滤
什么是遍历? jQuery 遍历,意为“移动”,用于根据其相对于其他元素的关系来“查找”(或选取)HTML 元素.以某项选择开始,并沿着这个选择移动,直到抵达您期望的元素为止. 下图展示了一个家族树. ...
- linux进程间通信-管道
一 管道的局限性 管道有两个局限性:(1)他是半双工(即数据只能在一个方向上流动).(2)它只能在具有公共祖先的进程之间使用.一个管道由一个进程创建,然后该 进程调用fork,此后父子进程之间就可该管 ...
- Redhat使用CentOS的Yum 网络源
Redhat 的更新包只对注册的用户生效,所以我们自己手动更改成CentOS 的更新包,CentOS几乎和redhat是一样的. 1.首先查看redhat 7.0系统本身所安装的那些yum 软件包:[ ...
- eclipse的包的加减号展开方式
这是win7系统下面 导航树的风格 可能你不太习惯 一个最简单的方法: 桌面新建个 eclipse 快捷方式--->右键属性--->兼容性 勾上以兼容模式运行这个程序
- 网络攻防比赛PHP版本WAF
这次去打HCTF决赛,用了这个自己写的WAF,web基本上没被打,被打的漏洞是文件包含漏洞,这个功能在本人这个waf里确实很是捉急,由于只是简单检测了..和php[35]{0,1},导致比赛由于文件包 ...
- 一个screen的简单配置。。
# Start message startup_message off defencoding utf- encoding utf- utf- shell bash hardstatus always ...
- PHP程序设计
① 在设计评论系统时,关于文章的一些属性,我们最好存放在一个内存缓存中,通过下面的设计每次仅需要查询一次即可获取文章的所有属性 class CommentsDoc{ private static $d ...