layout_weight 的解释及使用
layout_weight 的解释及使用
转自:http://my.oschina.net/jsan/blog/191492
在Android的控件布局中,有一个奇葩的 layout_weight 属性,定义如下:
layout_weight : 用于指定剩余空闲空间的分割比例。用法:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<LinearLayout android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_height" android:layout_weight="1" android:text="888"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_height" android:layout_weight="1" android:text="999999"/></LinearLayout> |
为什么说是奇葩呢?
以上面的布局代码为例,TextView-888 和 TextView-999999 是横向排列的2个控件,它们的layout_weight="1",说明这2个控件平分了所在LinearLayout的剩余的空闲空间, 我们很容易的就误认为这2个控件平分了水平方向的空间,即:各自占据了 50% 的宽度。
其实这是错误的,而是:TextView-999999控件所占据的宽度 > TextView-888所占据的宽度。因为999999字符占据的宽度大于888占据的宽度,即:w(999999) + 1/2空闲空间 > w(888) + 1/2空闲空间。
这就是它奇葩的地方,很容易就让我们一直误认为是整个控件分割空间。到这里,大家一定会认为,这样的话,layout_weight 这个属性就没有什么意义了,原以为它可以分配空间呢,原来只是分割剩余空闲空间。
其实,呵呵,layout_weight 是可以用来,这样比如2个控件的 layout_weight="1" 就可以各自50%平分整个空间了,因为:0 + 1/2空闲空间 = 0 + 1/2空闲空间
这是一个小技巧,也是非常实用的一个实用layout_weight分割方案:定义控件的 layout_width="0dp" 或 layout_height="0dp" 配上 layout_weight 就可以实现对整个空间的比例分割了。
下面定义了2个控件的 layout_width="0dp", layout_weight="1",实现了水平方向50%平均分割:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<LinearLayout android:orientation="horizontal"> <TextView android:layout_width="0dp" android:layout_height="wrap_height" android:layout_weight="1" android:text="888"/> <TextView android:layout_width="0dp" android:layout_height="wrap_height" android:layout_weight="1" android:text="999999"/></LinearLayout> |
下面定义了2个控件的 layout_height="0dp", layout_weight="1",实现了竖直方向50%平均分割:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<LinearLayout android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="0dp" android:layout_weight="1" android:text="888"/> <TextView android:layout_width="wrap_content" android:layout_height="0dp" android:layout_weight="1" android:text="999999"/></LinearLayout>
|
使用Layout_weight时空间分配的计算方法:http://mobile.51cto.com/abased-375428.htm
layout_weight 的解释及使用的更多相关文章
- 奇葩属性:layout_weight 的解释及使用
在Android的控件布局中,有一个奇葩的 layout_weight 属性,定义如下: layout_weight : 用于指定剩余空闲空间的分割比例.用法: 01 <LinearLayout ...
- [Android学习笔记]LinearLayout布局,剩余空间的使用
转自:http://segmentfault.com/q/1010000000095725 如果使得一个View占用其父View的剩余空间? 答案是使用:android:layout_weight = ...
- Android layout_weight的用法
android:layout_weight是指LinearLayout先给里面的控件分配完大小之后剩余空间的权重. 下面通过举例说明: <LinearLayout xmlns:android=& ...
- android:layout_weight越大所占比例越大和越大所占比例越小的两个例子
摘要: 我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3907146.html,享受整齐的排版.有效的链接.正确的代码缩进.更好的 ...
- Android_Fragment_Fragment具体解释
Android_Fragment_Fragment具体解释 分类: Android基础2013-10-03 08:23 92人阅读 评论(0) 收藏 举报 AndroidFragmentFragmen ...
- Android 布局学习之——Layout(布局)具体解释二(常见布局和布局參数)
[Android布局学习系列] 1.Android 布局学习之--Layout(布局)具体解释一 2.Android 布局学习之--Layout(布局)具体解释二(常见布局和布局參数) ...
- Android基础笔记(十)- 帧动画、补间动画具体解释、对话框
帧动画 补间动画Tween Animation 对话框以及面试中的注意点 帧动画 帧动画非常easy,我们首先看一下Google官方解释This is a traditional animation ...
- android:layout_weight属性的使用方法总结
原创文章,转载请注明出处http://www.cnblogs.com/baipengzhan/p/6282826.html android:layout_weight属性可以和其他属性配合使用,产生多 ...
- 【转】安卓布局:layout_weight的理解
android:layout_weight详细分析介绍: 布局文件是:<?xml version="1.0" encoding="utf-8"?>& ...
随机推荐
- SignalR实现实时日志监控
.net SignalR实现实时日志监控 摘要 昨天吃饭的时候,突然想起来一个好玩的事,如果能有个页面可以实时的监控网站或者其他类型的程序的日志,其实也不错.当然,网上也有很多成熟的类似的监控系统 ...
- 解决nginx上传模块nginx_upload_module传递GET参数
解决nginx上传模块nginx_upload_module传递GET参数的方法总结 最近用户反映我们的系统只能上传50M大小的文件, 希望能够支持上传更大的文件. 很显然PHP无法轻易实现大文件上传 ...
- Oracle EBS-SQL (OM-6):打开订单.sql
/*打开头*/ update oe_order_headers_all t set t.flow_status_code = 'BOOKED', t.open_flag = 'Y' where t.o ...
- Asp.Net MVC3 简单入门第一季(三)详解Controller之Filter
前言 前面两篇写的比较简单,刚开始写这个系列的时候我面向的对象是刚开始接触Asp.Net MVC的朋友,所以写的尽量简单.所以写的没多少技术含量.把这些技术总结出来,然后一简单的方式让更多的人很好的接 ...
- php IP string与整型互换
PHP中有内置函数ip2long可以将ip地址转换整型. 使用long2ip把整型转换回ip地址 例子: $ip = '58.6.207.207'; $ip_int = ip2long($ip); e ...
- Windows XPE 安装
to liuxiyao: 出现这样的问题我推断是由于系统有一些必须的dll类库没有被build进系统中,你在构建时多加入一些系统组件试试.(我想通过评论发可是发了5.6遍CSDN没反应,就写到这里吧, ...
- 【线段树求逆序数】【HDU1394】Minimum Inversion Number
题目大意: 随机给你全排列中的一个,但不断的把第一个数丢到最后去,重复N次,形成了N个排列,问你这N个排列中逆序数最小为多少 做法: 逆序数裸的是N^2 利用线段树可以降到NlogN 具体方法是插入一 ...
- 读书笔记一 Java程序员的基本修养(数组及其内存管理)
1.1 数组初始化 1.1.1 java数组是静态的 java数组被初始化之后,该数组所占的内存空间.数组长度都是不可变的. java程序中的数组必须经过初始化才可使用. 数组的初始化有两种方式: 1 ...
- c#中的数据类型简介
一.C#中的变量和常量 C#中用于定义常量的方式有两种一个使用const关键字,一个是用readonly关键字.使用const定义的常量叫静态常量(compile-time constant),用re ...
- poj2752Seek the Name, Seek the Fame
Description The little cat is so famous, that many couples tramp over hill and dale to Byteland, and ...