在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先满足第一个组件的长和宽,不管后面几个组件是否还有显示的空间)

变变变:

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

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

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

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

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

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

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

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

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

变不变:

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

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

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

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

变不不:

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

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

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

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

不不不:

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

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


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

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

</LinearLayout>

不不变:

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

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

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

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

不变不:

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

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

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

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

不变变:

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

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

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

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

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

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

layout_height 同 layout_width.


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

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

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

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

水平线性按比例分配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(安卓)组件长宽比重设置总结
  2. AndroidUI组件之ListView小技巧
  3. android:gravity和android:layout_Gravity的区别
  4. AndroidUI组件之ListView小技巧
  5. AndroidUI组件之ListView小技巧
  6. android android:gravity 和 android:layout_Gravity属性
  7. Android(安卓)Studio基础篇
  8. Android第二天
  9. Android简介

随机推荐

  1. SWIG与JAVA的交互指南一
  2. 安卓和机器学习 -- 学习周记{第1周}
  3. Android系统权限
  4. Android(安卓)Studio使用Git提交、分支、
  5. android设备静默升级
  6. Android(安卓)设备在Play Store无法搜索
  7. Android时光轴实现淘宝物流信息浏览效果
  8. Android导出与使用带资源的lib包
  9. Android(安卓)进阶——高级UI必知必会之
  10. Android(安卓)Studio之ListView的用法(上)