在android中的全部组件都有android:layout_height,android:layout_width,android:layout_weight这三个属性所以今天写了这篇总结,总结关于组件布局的问题。


各个属性可以设置的值:

android:layout_height:wrap_contentmatch_parentfill_parent),px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), mm (millimeters)

android:layout_width:wrap_contentmatch_parentfill_parent),px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), mm (millimeters)

android:layout_weight:整数

针对3种情况展开讨论:

假想一个列表框,需要放置3个组件,每个组件的长度可以是需要变化的(如TextView随文本的长度变化),或者是长度固定的(如ImageView组件)。

     
每一个格子里放一种组件,所以有8种情况,针对这8种情况分别对这3个组件进行这3个属性的配置:

(在同一个LinearLayout如果不设比重则LinearLayout先满足第一个组件的长和宽,不管后面几个组件是否还有显示的空间)

变变变:

            android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

                    android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="阿斯顿发萨达发萨达发萨达发射的阿斯顿发阿斯顿发" />

                    android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="阿斯顿发萨达发萨达发萨达发射的阿斯顿发阿斯顿发" />

                    android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="阿斯顿发萨达发萨达发萨达发射的阿斯顿发阿斯顿发" />
   

变变不:(image宽度不在比重分配的尺寸之中)

            android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" >

                    android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="1的阿斯顿发阿sadfasdfasdfsadfsadfsd斯顿发" />

                    android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="3"
            android:text="2顿发阿斯asdfa sdfadsfsdfasdfasdfasdfadsf顿发" />

                    android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@drawable/jiantou1" />
   

变不变:

            android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" >

                    android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="1的阿斯顿发阿sadfasdfasdfsadfsadfsd斯顿发" />

                    android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@drawable/jiantou1" />

                    android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="3"
            android:text="2顿发阿斯asdfa sdfadsfsdfasdfasdfasdfadsf顿发" />
   

变不不:

              android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" >

                    android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="2顿发阿斯asdfa ssdfsdgsdfgdfadsfsdfasdfasdfasdfadsf顿发" />

                    android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@drawable/jiantou1" />

                    android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@drawable/jiantou1" />
   

不不不:

           android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" >

                    android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@drawable/jiantou1" />


                    android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@drawable/jiantou1" />

                    android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@drawable/jiantou1" />
      
   

不不变:

        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" >

                    android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@drawable/jiantou1" />

                    android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@drawable/jiantou1" />

                    android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="2顿发阿斯asdfa ssdfsdgsdfgdfadsfsdfasdfasdfasdfadsf顿发" />
   

不变不:

             android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" >

                    android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@drawable/jiantou1" />

                    android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="2顿发阿斯asdfa ssdfsdgsdfgdfadsfsdfasdfasdfasdfadsf顿发" />

                    android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@drawable/jiantou1" />
   

不变变:

            android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" >

                    android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@drawable/jiantou1" />

                    android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="2顿发阿斯asdfa ssdfsdgsdfgdfadsfsdfasdfasdfasdfadsf顿发" />

                    android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="2顿发阿斯asdfa ssdfsdgsdfgdfadsfsdfasdfasdfasdfadsf顿发" />
   

细心的读者可能已经明白其中的规律!

在layout_width设置为fill_parent的时候,layout_weight所代表的是你的控件要优先尽可能的大,但这个大是有限度的,即fill_parent.
在layout_width设置为wrap_content的时候,layout_weight所代表的是你的控件要优先尽可能的小,但这个小是有限度的,即wrap_content.

layout_height 同 layout_width.


           android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" >

                    android:id="@+id/textView2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:text="2顿发阿斯asdfa ssdfsdgsdfgdfadsfsdfasdfasdfasdfadsf顿发" />

                    android:id="@+id/textView2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="2顿发阿斯asdfa ssdfsdgsdfgdfadsfsdfasdfasdfasdfadsf顿发" />

                    android:id="@+id/textView2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="2顿发阿斯asdfa ssdfsdgsdfgdfadsfsdfasdfasdfasdfadsf顿发" />
   

水平线性按比例分配android:layout_weight="1"时也可将组件的android:layout_width="0dp",效果同android:layout_width="wrap_content",按正比例分配除去固定分配掉的尺寸所剩余的尺寸。


当View的width,height为wrap_content或者0dp/px/sp时,若设定了weight,那么layout就会从剩余的未确定的layout中,根据weight的总和,也就是weightSum来计算需要为这样的View设置多少长宽。
width = leftWidth * weight / weightSum;
height = leftHeight * weight / weightSum;
另外weight的计算和布局,在linearLayout中Onlayout和OnMeasure函数。

更多相关文章

  1. Android中gravity与layout_gravity的区别
  2. android中的六大布局
  3. Android开发——控件基础(五)RadioButton组件:属性、示例代码
  4. android:gravity 与android:layout_gravity
  5. layout_gravity与android:gravity有什么区别
  6. android中的六大布局
  7. Android的系统体系结构
  8. Android(安卓)Studio-解决Fetching android sdk component infor
  9. Android(安卓)组件长宽比重设置总结

随机推荐

  1. 使用SQL批量替换语句修改、增加、删除字
  2. SQL Server 存储过程遇到“表 ''
  3. 浅谈数据库优化方案
  4. Win10 64位安装个人版SQL2000图文教程
  5. SQL Server触发器和事务用法示例
  6. sqlserver进程死锁关闭的方法
  7. 获取SqlServer存储过程定义的三种方法
  8. SQL注入全过程深入分析
  9. SQL Server表中添加新列并添加描述
  10. SQL Server并行操作优化避免并行操作被抑