layout_weight体验(实现按比例显示)
在android开发中LinearLayout很常用,LinearLayout的内控件的android:layout_weight在某些场景显得非常重要,比如我们需要按比例显示。android并没用提供table这样的控件,虽然有TableLayout,但是它并非是我们想象中的像html里面的table那么好用,我们常用ListView实现table的效果,但是列对齐确比较麻烦,现在用LinearLayout及属性android:layout_weight能很好地解决。下面我们共同体验下layout_weight这个属性。
一、LinearLayout内的控件的layout_width设置为"wrap_content",请看一下xml配置:
- <LinearLayout
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_weight="1"
- >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="fill_parent"
- android:layout_weight="1"
- android:background="#aa0000"
- android:gravity="center"
- android:text="1"/>
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="fill_parent"
- android:layout_weight="2"
- android:background="#00aa00"
- android:gravity="center"
- android:text="1"/>
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="fill_parent"
- android:layout_weight="3"
- android:background="#0000aa"
- android:gravity="center"
- android:text="1"/>
- </LinearLayout>
可以看到这三个TextView是按照1:2:3的比例进行显示的,这样看来似乎可以实现按照比例显示了,但是有个问题,如果TextView内的文本长度一同那么较长文本的TextView会宽度会有所增加,见下面配置及效果:
- <LinearLayout
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_weight="1">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="fill_parent"
- android:layout_weight="1"
- android:background="#aa0000"
- android:gravity="center"
- android:text="1111111111111111111111111111111111111111111"/>
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="fill_parent"
- android:layout_weight="2"
- android:background="#00aa00"
- android:gravity="center"
- android:text="2"/>
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="fill_parent"
- android:layout_weight="3"
- android:background="#0000aa"
- android:gravity="center"
- android:text="3"/>
- </LinearLayout>
效果如下:
这样看来我们所需要的按比例又无法实现了,经过满天地google终于找到了解决方案,就是设置layout_width设置为"wrap_content"。配置及效果见下:
- <LinearLayout
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_weight="1">
- <TextView
- android:layout_width="0dp"
- android:layout_height="fill_parent"
- android:layout_weight="1"
- android:background="#aa0000"
- android:gravity="center"
- android:text="1111111111111111111111111111111111111111111"/>
- <TextView
- android:layout_width="0dp"
- android:layout_height="fill_parent"
- android:layout_weight="2"
- android:background="#00aa00"
- android:gravity="center"
- android:text="2"/>
- <TextView
- android:layout_width="0dp"
- android:layout_height="fill_parent"
- android:layout_weight="3"
- android:background="#0000aa"
- android:gravity="center"
- android:text="3"/>
- </LinearLayout>
这样终于达到我们的按比例显示的效果了,感觉很是奇怪,android开发框架的大佬们有时候设计确实有点匪夷所思。
二、LinearLayout内的控件的layout_width设置为"fill_parent",请看一下xml配置:
- <LinearLayout
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_weight="1">
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_weight="1"
- android:background="#aa0000"
- android:gravity="center"
- android:text="1"/>
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_weight="2"
- android:background="#00aa00"
- android:gravity="center"
- android:text="2"/>
- </LinearLayout>
奇怪吧,整个宽度平分3块,第一个TextView占了两块,这样看来weight值越小的优先级越大。只有两个TextView似乎看出些道理,那么让我们看看三个是什么效果:
- <LinearLayout
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_weight="1">
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_weight="1"
- android:background="#aa0000"
- android:gravity="center"
- android:text="1"/>
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_weight="2"
- android:background="#00aa00"
- android:gravity="center"
- android:text="2"/>
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_weight="3"
- android:background="#0000aa"
- android:gravity="center"
- android:text="3"/>
- </LinearLayout>
layout_weight体验(实现按比例显示)的更多相关文章
- 转 layout_weight体验(实现按比例显示)
http://www.cnblogs.com/zhmore/archive/2011/11/04/2236514.html 在android开发中LinearLayout很常用,LinearLayou ...
- Ui篇--layout_weight体验(实现按比例显示)
在android开发中LinearLayout很常用,LinearLayout的内控件的android:layout_weight在某些场景显得非常重要,比如我们需要按比例显示.android并没用提 ...
- js控制 固定框架内图片 按比例显示 以及 占满框架 居中显示
js控制 固定框架内图片 等比例显示 以及 占满框架 纵横居中显示 通过设置 js函数 fitDiv里面var fit的值就好 function fitDiv (obj) { var target_w ...
- 让自定义view宽高成比例显示
有时候我们自定义一个View,比如ImageView,我们需要让它宽高按照一定的比例显示,例如在ImageView在GridView中显示,GridView设置了3列,由于ImageVIew的宽度会根 ...
- 页面布局 ——图片自动按比例显示&&图片随外部div的增大而按比例增大
图片按比例显示,分为两种情况. 1.空的div内加图片 <div class="emty"><img src="img/my.png"> ...
- 利用 :before 特性实现图片按比例显示
好吧,想不到自称布局小沙弥的我会被图片按比例显示给尴尬到. 设计师跟我说,这里的图要按 750x330 的,好吧,但这图是屏宽呀,屏幕宽度会变化的,那高度也会不定咯, 要么裁图片(工作量大),要么给定 ...
- 最新javascript自动按比例显示图片,按比例压缩图片显示
最新javascript自动按比例显示图片,按比例压缩图片显示 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//E ...
- CSS3可伸缩框属性,可用于等分显示子元素或按比例显示子元素的大小
使用方法跟Android的android:layout_weight属性类似.可类比Android中的使用方法.这样比較好记,因为眼下全部浏览器都不支持大部分的属性,所以全部的属性都须要加上Firef ...
- android LinearLayout android:layout_weight 作用,固定比例
android 中的 LinearLayout 是线性布局有水平布局horizontal 垂直布局vertical .本文针对 水平布局horizontal 布局的weight属性做一个标记,以免 ...
随机推荐
- ios 代码截屏模糊问题解决办法
我们常用的截图方法如下所示: //尺寸是按照 UIGraphicsBeginImageContext(CGSizeMake(, )); //currentView 当前的view 创建一个基于位图的图 ...
- 移动端 viewport设置
<meta name="viewport" content="" /> width [pixel_value | device-width] wid ...
- java 远程调试
在java 命令上加入 -Xdebug -Xrunjdwp:transport=dt_socket,address=8001,server=y suspend=y 配置的端口时候要保证没有被占用
- Linux chmod command
@ chmod改变一个或多个文件的存取模式 chmod [options] mode files 只有文件属主或特殊用户才能使用该功能来改变文件存取模式.mode可以是数字形式或who opcode ...
- [转]WPF 获取程序启动路径
本文转自:http://hi.baidu.com/lilipangtou/item/f1b7488e3c92c4d05e0ec1ab 在Windows Form程序中,获取自身的启动目录是非常容易的, ...
- [转]关于position 的 static、relative、absolute、fixed、inherit
本文转自:http://www.56770.com/faq/list/?id=410 position 有五个值:static.relative.absolute.fixed.inherit. sta ...
- Linux 驱动分类 与访问技术
驱动开发概述 1.驱动分类 1.1 常规分析法 1.1.1 字符设备 字符设备是一种按字节来访问的设备,字符驱动则负责驱动字符设备, 这样的驱动通常实现open, close, read和wri ...
- 10 Interesting Linux Command Line Tricks and Tips Worth Knowing
I passionately enjoy working with commands as they offer more control over a Linux system than GUIs( ...
- 初见,Devexpress
[绥江一百]http://www.sj100.net 欢迎,进入绥江一百感谢点击[我的小网站,请大家多 ...
- ASP.NET缓存全解析3:页面局部缓存 转自网络原文作者李天平
有时缓存整个页面是不现实的,因为页的某些部分可能在每次请求时都需要变化.在这些情况下,只能缓存页的一部分.顾名思义,页面部分缓存是将页面部分内容保存在内存中以便响应用户请求,而页面其他部分内容则为动态 ...