• Layout_weight实践效果小结


    androidlayout_weight

    是用于给一个线性布局(LinearLayout)中的诸多视图的重要度赋值。

    所有的视图都有一个layout_weight值,默认为零,意思是需要显示多大的视图就占据多的的屏幕空间。若赋值高于零又是怎么样的呢?

    在网上有很多关于layout_weight的学习资料,网上介绍的都大同小异,以为该学到的都学到的时候,一实践就发现了一些问题。以下一段是网上关于layout_weight值大于零的显示效果的说法。

    如果一行中有两个等长的文本框,那么他们的android:layout_weight值可以是同为1。如果一行中有两个不等长的文本框,那么他们的android:layout_weight值分别为12,那么第一个文本框将占据剩余空间的三分之二,第二个文本框将占据剩余空间中的三分之一。android:layout_weight遵循数值越小,重要度越高的原则。

    网上说法都没有提到layout_width/layout_height的值,事实上layout_weight的属性值跟layout_width/layout_height有很大的关系,下面是我结合网上学习和个人实践后作出的有关layout_weight使用的小结。当然正不正确读者自己实践证明就知道。

    首先一点LinearLayout中要让layout_weight生效,需要父层或父父层的相应layout_width/layout_height=fill_parent”,否则“wrap_content”会压缩到最小足够空间。

    第二所有的视图都有一个layout_weight值,默认为零,需要显示多大的视图就占据多的的屏幕空间。

    第三:当layout_weight值大于零是情况有二。以三个并列的文本框为例说明如下,其中layout_width影响layout_weight,代码如下:

    <?xml version="1.0" encoding="UTF-8"?>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:orientation="horizontal" >

     

        <TextView

            android:background="#ff0000"

            android:layout_width="**"

            android:layout_height="wrap_content"

            android:text="1"

            android:textColor="@android:color/white"

            android:layout_weight="1"/>

        <TextView

            android:background="#cccccc"

            android:layout_width="**"

            android:layout_height="wrap_content"

            android:text="2"

            android:textColor="@android:color/black"

            android:layout_weight="2" />

         <TextView

            android:background="#ddaacc"

            android:layout_width="**"

            android:layout_height="wrap_content"

            android:text="3"

            android:textColor="@android:color/black"

            android:layout_weight="3" />

    </LinearLayout>

    情况一:

    三个文本框的都是layout_width=fill_parent”时,其权重赋值遵循数值越小,重要度越高的原则。这种情况下,没有设置layout_weight的话,第一个组件将占用余下的空间,那么后面的组件就显示不出来。

    •    都为1的话就是平分
      Layout_weight实践效果小结

     

      (图一)

     

    •   123的话,只有前面两个文本框显示,比例为21(如图一)。Layout_weight实践效果小结

     

      (图二)

    •   122的话,分成5份,三个文本框都显示,比例为3:1:1(如图三);Layout_weight实践效果小结
      (图三)
    •   133的话,分成7份,三个文本框都显示,比例为5:1:1Layout_weight实践效果小结

     

      (图四)

     

       注意:这种情况适合两个组件设置权重,多了的话不好预知显示结果,设置不好的话有些组件无法显示。 

    情况二:

    三个文本框的都是layout_width=wrap_content”时,其值表示占用剩余空间的比例。

    •   123的话,分成6份,三文本框显示,比例1:2:3(如图五)Layout_weight实践效果小结

     (图五)

    情况三:

    当三个文本框中的layout_width的值既有fill_parent又有wrap_content时,以fill_parent优先。

     

     

    结束语不同情况显示的都不同,有些虽然别人介绍,我们以为懂了,可是就有那么多小细节没有考虑到导致结果相差甚远,所以每个效果只有自己试了才知道,只有出错了才了解,只有成功了才确定。

  • 相关阅读:
    【NOIP】提高组2015 运输计划
    【BZOJ】1635: [Usaco2007 Jan]Tallest Cow 最高的牛
    【51nod】1766 树上的最远点对
    【BZOJ】2054: 疯狂的馒头
    【SRM20】数学场
    【Luogu】P3930 SAC E#1
    【Luogu】P3927 SAC E#1
    【Luogu】 P3928 SAC E#1
    【Codeforces】868C. Qualification Rounds
    【CodeForces】866D. Buy Low Sell High
  • 原文地址:https://www.cnblogs.com/zhwl/p/2403054.html
Copyright © 2020-2023  润新知