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值的和。

转载于:https://www.cnblogs.com/wikiki/p/4511845.html

转 layout_weight体验(实现按比例显示)相关推荐

  1. 自定义控件:等比例显示控件RatioLayout

    我们经常碰到服务器返回的图片比例大小是一样的,但是分辨力却是不一样的.这时候,就会遇到显示效果的问题.例如,图1和图2都是宽高比例相等,但是分辨率大小不一样的图片,应该按照比例显示,使用等比例显示控件 ...

  2. 让自定义view宽高成比例显示

    有时候我们自定义一个View,比如ImageView,我们需要让它宽高按照一定的比例显示,例如在ImageView在GridView中显示,GridView设置了3列,由于ImageVIew的宽度会根 ...

  3. php让手机全屏,手机网站按比例显示铺满手机全屏/网站两边不留白

    如何phpweb手机网站按比例显示铺满手机全屏/网站两边不留白? PHPWEB手机网站,一般手机网站框架宽度为320.640,大多数是这两个宽度,然而在大屏幕手机比如安卓手机上,720分辨率的情况下, ...

  4. 微信小程序——image图片自适应宽度比例显示的方法

    微信小程序的组件image是用来显示图片的,它有一下几个属性: 1.src              图片资源地址 2.mode          图片裁剪.缩放的模式 3.binderror     ...

  5. html 设置图片显示比例,css怎么实现图片在页面上以相同等比例显示缩放?(示例)...

    在我们浏览页面时或者实际工作中,偶尔会遇到图片缩放的问题.那么本篇文章就给大家介绍关于css 图片等比例缩放即css图片等比例显示的问题.希望对有需要的朋友有所帮助. css图片等比例显示具体代码示例 ...

  6. ViewPager按比例显示图片(显示下一张图片的一部分)

    重写PageAdapter的方法: public float getPageWidth(int position) { if (position == 0 || position == 2) { re ...

  7. Android自定义控件:imageview重写onMeasure方法实现图片按指定比例显示,拉伸永不变形,解决屏幕适配问题

    使用ImageView会遇到的问题 在Android应用中,都少不了图片的显示,ImageView,轮播图,ViewPager等等,很多都是来显示图片的,比如一个广告条的轮播效果,参看博客:广告条效果 ...

  8. css实现内容按一定比例显示,如4:3,16:9

    css实现内容按一定比例显示,如4:3,16:9 参考了此文章 css实现长宽比的几种方案

  9. android 图片百分比显示,Android编程实现等比例显示图片的方法

    本文实例讲述了Android编程实现等比例显示图片的方法.分享给大家供大家参考,具体如下: 在android中,由于密度的影响,如果想得到图片的宽高是不行的,具体为什么我就大概说一下,具体的请搜索度娘 ...

  10. C++ MFC picture control控件按比例显示任意图片文件(jpg、bmp等)

    前段时间做大一专业实训,用MFC做一个图片查看类程序,对于从来没有学过C++的我们来说显示一张图片就是一件很难的事情了,作为小组里唯一一个MFC设计者,我从网上有很多关于MFC图片控件显示的文章,但大 ...

最新文章

  1. 拿高薪的程序员,都学习了什么编程语言?
  2. linux Fedora搭建hadoop平台总结
  3. MySQL数据库(十) 一一 数据库的导出和导入
  4. 【Android 逆向】ART 脱壳 ( dex2oat 脱壳 | aosp 中搜索 dex2oat 源码 | dex2oat.cc#main 主函数源码 )
  5. 云上创新 | 阿里云边缘云场景化商业实践
  6. 在word中给公式添加序号
  7. [活动 3.30]MAUI 跨平台应用开发实战
  8. 路由器有一个android设备连接不上,Android http连接 – 多个设备无法连接同一台服务器...
  9. C语言 笔试 各个字节数,C语言笔试试题及答案
  10. jpype,jpython调用jar包中jdk的问题.
  11. 20-21-2网络管理quiz5
  12. Vue学习笔记进阶篇——多元素及多组件过渡
  13. AngularJS 实现页面滚动到底自动加载数据的功能
  14. 08 域控 架构主机 转移_win2000域控迁移至win2008
  15. python opencv中的imwrite函数_python – OpenCV imwrite函数导致’undefined symbol’
  16. Python爬虫搜索全网音乐并下载
  17. 锤子android 7,锤子新机坚果Pro配置放出:骁龙626处理器、Android 7.1.1系统
  18. 论文阅读笔记:A Network-based End-to-End Trainable Task-oriented Dialogue System
  19. Windows10如何删除自带输入法,设置默认输入法
  20. BZOJ3693:圆桌会议(Hall定理)

热门文章

  1. 计算机英语板卡,数据采集卡
  2. 2017年软件工程第八次作业-互评Alpha版本
  3. python学习之记事本
  4. FCKeditor 2.6.4在ASP.NET中的配置方法
  5. 见过最牛的GIF图片。
  6. 【TDA4系列】CCS 最新版本安装与教程地址
  7. pandas筛选某个列值是否位于某个列表内
  8. Mysql优化(出自官方文档) - 第七篇
  9. oracle 备份恢复 12oracle逻辑备份恢复补充
  10. Mysql基础之 事务