`
qilixiang012
  • 浏览: 201978 次
文章分类
社区版块
存档分类
最新评论

Android 组件长宽比重设置总结

 
阅读更多

在android中的全部组件都有android:layout_heightandroid:layout_widthandroid: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函数。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics