转 layout_weight体验(实现按比例显示)

http://www.cnblogs.com/zhmore/archive/2011/11/04/2236514.html

在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>

效果:

什么意思?第三个TextView丢掉了,很是奇怪,让我们再试一个,把weight分别改为2,3,4的看看效果:

这个效果让人很困惑,我一直想寻求出一个确切的比例公式,但是至今未找到。有哪位大神能搞定的话忘不吝赐教。

虽然这个android:layout_weight属性很怪异,但幸运的是我们达到了目标:

  按比例显示LinearLayout内各个子控件,需设置android:layout_width="0dp",如果为竖直方向的设置android:layout_height="0dp"。在这种情况下某子个控件占用LinearLayout的比例为:本控件weight值 / LinearLayout内所有控件的weight值的和。

时间: 2024-08-28 01:46:52

转 layout_weight体验(实现按比例显示)的相关文章

layout_weight体验(实现按比例显示)

在android开发中LinearLayout很常用,LinearLayout的内控件的android:layout_weight在某些场景显得非常重要,比如我们需要按比例显示.android并没用提供table这样的控件,虽然有TableLayout,但是它并非是我们想象中的像html里面的table那么好用,我们常用ListView实现table的效果,但是列对齐确比较麻烦,现在用LinearLayout及属性android:layout_weight能很好地解决.下面我们共同体验下layo

CSS3可伸缩框属性,可用于等分显示子元素或按比例显示子元素的大小

用法跟Android的android:layout_weight属性类似,可类比Android中的用法,这样比较好记,由于目前所有浏览器都不支持大部分的属性,所以所有的属性都需要加上Firefox.Safari.Opera 以及 Chrome支持替代的-moz-和-webkit-前缀,现在基本上所有的CSS3属性都应该这样做,即每个属性都需要至少设置三个,参考下面的例子. 父容器属性: display:box;当定义了这个属性时,子元素想居中显示时margin:0px auto是无效的,需使用t

图片的按比例显示

1.获取手机当前分辨率 1.1 1 WindowManager wm=(WindowManager)getSystemService(WINDOW_SERVICE); 2 Point point=new Point(); 3 wm.getDefaultDisplay().getSize(point); 4 int height=point.x; 5 int weight=point.y; 1.2 1 WindowManager wm = (WindowManager)getSystemServi

利用 :before 特性实现图片按比例显示

好吧,想不到自称布局小沙弥的我会被图片按比例显示给尴尬到. 设计师跟我说,这里的图要按 750x330 的,好吧,但这图是屏宽呀,屏幕宽度会变化的,那高度也会不定咯, 要么裁图片(工作量大),要么给定高(图片会比例不对,或者用 background 显示不全),来体会一下... 当宽度变化时,立马就呵呵了,一脸懵逼... 后来吧,在研究大量图片数据加载优化问题时,去调研了下淘宝天猫京东,然后一不小心发现了 :before 这种布局方式,先看一眼代码 .img { position: relati

最新javascript自动按比例显示图片,按比例压缩图片显示

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Typ

13.按比例显示图片、自定义属性、测量

有时候服务器返回的图片有可能宽高是不一样的,所以需要按照一定宽高比例去显示,修改专题界面 自定义属性 <resources> <declare-styleable name="com.itheima.googleplay.view.RatioLayout"> <attr name="ratio" format="float"></attr> </declare-styleable> &l

GTK编程:将图片文件按指定比例显示

实现功能:将指定的图片文件按指定比例在窗体中显示 注意:这段代码没有信号处理的实现,当点击关闭窗体的时候,虽然窗体关闭了,但程序并没有真正退出. /* File: SizeShow.c * Date: Mon Mar 27 15:36:09 HKT 2015 * Describe: Show the picture with the specified size * Autor: won */ #include <stdio.h> #include <gtk/gtk.h> //指定

视频播放按比例显示

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" session="false" %> <% // 获取视频播放名称 String src = request.getParameter("name"); String name = "video\\"+

图片居中按比例显示、鼠标滚动缩放、鼠标拖动平移

1.为了居中显示,考虑到div不好设置,用table做边框 缩放原理:调整图片宽高.定位left.top: 平移:鼠标事件位置.定位left.top. 目前贴出实现代码,具体研究内容再做补充 下面是源码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <h