layout_weight体验(实现按比例显示)_勤劳的小疯子的博客-程序员秘密_layout_width 按比例

技术标签: layout  xml  android  listview  google  table  

注:LinearLayout中的TextView按比例显示的时候,layout_width="0dp"或者layout_height="0dp"

 

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

注:原文链接http://www.cnblogs.com/zhmore/archive/2011/11/04/2236514.html

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/zzf112/article/details/7034412

智能推荐

keil中自己定义的枚举报错error: #101: “RS485_TX“ has already been declared in the current scope_super尚的博客-程序员秘密

因为是把62行的#endif放到了55行那里 导致后边的自己写的东西报错。把#endif放最后就好了

<li> 点击变色</li>_chuyong2135的博客-程序员秘密

$("#rotate ul li a").click(function(){ //tab变换 $("#rotate ul li a").removeClass('selec1'); $(this).addClass('selec1'); }); &...

Windows下安装GCC G++ 并使用CMake生成Makefile文件,并使用GDB进行调试_想游泳的鱼的博客-程序员秘密

想在Windows下使用GCC G++编译C程序,并想使用CMake来生成Makefile文件,在这个过程中碰到了一些问题,特记录下来。安装CMake这个很简单,去CMake的官网下载一个CMake的Windows安装包,点击安装即可。在安装结束的时候,最好选择 设置环境变量CMake官网 https://cmake.org/安装GCC G++首先需要去安装mingW,参考这...

PBRT学习笔记:在单位圆内部均匀采样_就不告诉你1111的博客-程序员秘密

在一个单位圆内部均匀采样,即在单位圆内部随机取一个采样点,而每个采样点被取到的概率都是1/Pi。这个问题看似非常简单,但是可能要比想象中复杂一点。这里的采样我们从canonical uniform variable开始,即我们只能够生成从0到1的随机数,而其分布是均匀分布的。 首先,最容易想到的方法就是rejection method。在单位圆的外部做一个Bounding Box,紧紧的包住这个单位圆。然后用两个随机点分别在两个轴向上取点,从而均匀的随机提取正方形内部的一个点。然后进行简单的测试,当这个

Typora入门教程_typora 突出行_心静下来了吗的博客-程序员秘密

Markdown标记笔记语言:学生时代,我们时如何学习的?课本:知识全面详细、知识条理不清晰、重难点不突出板书:知识条理清晰、重难点突出+扩展、面向全体同学笔记:知识条理清晰、重难点突出+深度扩展、符合个人指定化需求笔记工具的选择:现如今比较好用的笔记工具就是记事本、Word、标记语言、思维导图、有道云笔记以及微软的OneNote!Markdown简介:Markdown是轻量级语言,能够以少量代码完成多个高级功能,它是由John Gruberis开发的语言,Typora是Markdown

ST7735旋转任意角度(90/180/360)_st7735怎么翻转_程皖Orz的博客-程序员秘密

以STM32驱动ST7735程序为例,在初始化阶段以下寄存器为控制屏幕旋转方向 LCD_WR_DATA(0x8A); LCD_WR_DATA(0xEE); LCD_WR_REG(0xC5); LCD_WR_DATA(0x0E); LCD_WR_REG(0x36); LCD_WR_DATA(0xC8); //Just this line(C0/00/A0/60...

随便推点

回复审稿意见_LeeOhe丶的博客-程序员秘密

https://www.jianshu.com/p/7edb6771594a【SCI修改稿回答审稿人意见范文模板】【回信】List of Responses0.1 Dear Editors and Reviewers:1.1 Thank you for your letter and for the reviewer's comments concering our manuscript “Title”(ID: ).1.2 Thank you very much for your .

关于javaBean中boolean类型变量的set和get_Herman-Hong的博客-程序员秘密

boolean isProductInPromotion = false;其的get、set方法如下public boolean isProductInPromotion() {  return isProductInPromotion; } public void setProductInPromotion(boolean isProductInPromoti

BAT九九八十一问,Redis、多线程、高并发、集合框架、数据库、JVM一个都不放过!..._蔚1的博客-程序员秘密

本场chat搜集了BAT九九八十一问(附答案),分别包括Redis、多线程、高并发、集合框架、数据库、JVM等内容,希望可以给正在准备学习面试的你有点收获。...

mysql导出身份证号后面有0_导出数据excel表--身份证号后三位是0--〉还原_励静合的博客-程序员秘密

CH模拟赛 还教室/* 区间操作,可以推一推式子,方差为平方的平均数-平均数的平方,维护区间和与区间平方和,平方和的维护方法类似,式子推一推就行了,注意约分 */ #include #i ...Fliptile 开关问题 poj 3279Fliptile Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4031 ...

ubantu16.04 Tensorrt-onnx 安装记录_-dgpu_archs_Coding-Prince的博客-程序员秘密

cmake Could NOT find Protobuf (missing: Protobuf_LIBRARIES Protobuf_INCLUDE_DIR)sudo apt-get install protobuf-compiler libprotobuf-devShapedWeights.hpp:25:21: fatal error: NvInfer.h: No such file or directoryinclude_directories(/home/wzp/project/trt/Te

CanMeng-WEB安全渗透三周年了!!!_CanMeng'Blog的博客-程序员秘密

CanMeng-Web安全渗透三周年啦!因此推出一套全新渗透安全课程.欢迎各位小伙伴们加入学习!本培训为私人中心百度首页第一页排名第五搜索首页第一页排名第九有任何问题都可以联系我.CanMeng个人博客.知乎.简书.CSDN等平台都会定时发布各类技术文章欢迎各位订阅+关注,共同努力进步.联系咨询Q1426470161...