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

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

大家好,又见面了,我是全栈君。在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>

 效果如下:

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

可以看到这三个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>

效果:

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

这样看来我们所需要的按比例又无法实现了,经过满天地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之layout_weight体验(实现按比例显示)

这样终于达到我们的按比例显示的效果了,感觉很是奇怪,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>

效果如下:

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

奇怪吧,整个宽度平分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>

效果:

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

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

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

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

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/121448.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 虚拟存储技术「建议收藏」

    虚拟存储技术「建议收藏」一.实现内存扩充的技术:(1)覆盖技术:在程序运行中,在不同时刻把同一个存储区分配给不同程序段和数据段,实现存储区共享。适用于连续存储(单一连续区分配,分区)如图BDG共享一个存储区(三个进程不同时发生),CEFH同理(2)交换技术(对换技术):1.定义:将内存中某进程的的程序和数据(全部或部分)写入外存的交换区,从而腾出内存空间给其他进程使用。2.相关涉及知识

    2022年9月26日
    1
  • 3d游戏建模全解[通俗易懂]

    3d游戏建模全解[通俗易懂]目前市面上随着3D游戏的兴起和VR的盛行,越来越多人对网络游戏越来越热衷,3D游戏建模设计师的需求也越来越广泛,市场缺口大,人才需求供不应求。但在大多数人的印象中,这个行业似乎很难入门,没有美术基础,好像丝毫没有机会进入这个行业。真的是这样吗?3D建模3D建模通俗来讲就是通过三维制作软件构建出具有三维数据的模型。在3DMAX中,建模各项最首要的就是感觉。需要感觉每个部件的大小。感觉各个部位所需要使用的材质、颜色等。需要把控整体的颜色效果。而这些可以说都和美术的基础挂钩的,尤其是颜色。颜色

    2022年5月11日
    42
  • JVM类加载过程

    1.JVM类加载过程1.概述从类的生命周期而言,一个类包括如下阶段:加载、验证、准备、初始化和卸载这5个阶段的顺序是确定的,类的加载过程必须按照这种顺序进行,而解析阶段则不一定,它在某些情况下可能在初始化阶段后在开始,因为java支持运行时绑定。2.类加载时机加载(loading)阶段,java虚拟机规范中没有…

    2022年4月4日
    57
  • 培训师电梯销售法则-三句半「建议收藏」

    培训师电梯销售法则-三句半「建议收藏」今天在北京一个神奇的培训中心开发一门无线接入技术的培训课件,开发完毕以后,进行了课程试讲,总体试讲的情况还是很不错的,但是在总结阶段,效果并不理想,因此辅导老师最后给出了一个电梯销售法则-三句半,下面首先介绍一下电梯销售法则: “麦肯锡30秒电梯理论”来源于麦肯锡公司一次沉痛的教训。  麦肯锡公司曾经得到过一次沉痛的教训:该公司曾经为一家重要的大客户做咨询。咨询结束…

    2022年5月27日
    38
  • java用户态和内核态「建议收藏」

    java用户态和内核态「建议收藏」在&lt;深入理解java虚拟机&gt;这本书上多次看到用户态和内核态两个名词,虽然大概能明白意思.但对于两者具体的定义和区别还是比较,特此查阅之后记录.内核态(KernelMode)与用户态(UserMode)内核态:CPU可以访问内存所有数据,包括外围设备,例如硬盘,网卡.CPU也可以将自己从一个程序切换到另一个程序用户态:只能受限的访问内存,且不允许访问外…

    2022年9月17日
    4
  • accumulate

    accumulate

    2021年12月8日
    70

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号