`
119568242
  • 浏览: 420208 次
  • 性别: Icon_minigender_1
  • 来自: 深圳/湛江
社区版块
存档分类
最新评论

[android]layout_weight 在layout_width 为fill_parent 与wrap_content 时的不同含义

 
阅读更多

 

转自:http://hi.baidu.com/ljlkings/blog/item/fa2a59803f839a82f603a6b2.html?timeStamp=1305190390481

 

 

 

我的话:

简单的说weight代表权重

在layout_width =fill_parent 时

组件填充是要尽可能的大 那么权重重的占领的位置就多。[既1的权重比2大] 最大可达到占领整个父view

 

在layout_width =wrap_contentt 时

组件填充是要尽可能的小 那么权重重的占领的位置就小。[既1的权重比2大,权重大的先选先满足条件]

最小可达到能报纸能容显示的最小

 

 

===========================原文==============

那首先分析一下當layout_width屬性設置為fill_parent的時候,即充滿父佈局,當然意思是這個控件要根據weight的設置盡可能的大,因此,依上例而論,button1的weight設為1,button2的weight設置為2.即button的優先級最高,因此,要填充父佈局就要button1先來填充,盡可能的大,那這個盡可能又是多少呢,這就要綜合layout裡其他控件的weight值了,然後做一下運算,button1佔據2/3,button2佔據1/3.你也可以把button2設置為一個非常大的數,比如2000,此時在Graphical Layout模式下可以看到button1填充滿了整個寬度,而看不到button2的影子,事實上button2還是存在的,你把鼠標指向button1的後面就可以看到一個長長的豎條,那個就是button2,已經非常非常小了。因此,在layout_width設置為fill_parent的時候,weight所代表的是你的控件要優先盡可能的大。

 

 

     接著是當layout_weight設置為wrap_content的時候,即適應內容的寬度,意思是這個控件要盡可能的小,只要能把內容顯示出來就可以了,同樣的,如果把button1和button2的layout_weight設置為wrap_content後,button1的weight為1,button2的weight為2.那麼button1要優先盡可能的小,而button2也要盡可能的小,只是優先級不一樣,因為設置了weight,所以這兩個控件總的寬度要填滿父佈局的寬度,所以就又要計算每個控件所佔據的大小,此時,button1的優先級較高,共有兩份,一份1/3,一份2/3,button1要盡可能的小,那button1當然要選1/3,因此,我們看到的效果反而是button2佔據的較大。這裡要說的是如果把權值同樣做如下設置:button1為1,button2為2000,那button1是不是就要佔據1/2000的空間呢?這麼理解就錯了,剛才說了,要盡可能的小,但這個小是有一個限度的,那就是wrap_content,就是還要是內容完完整整的顯示出來,同樣的,盡可能的大也是有一個限度的,那就是父佈局的寬度。因此,在layout_width設置為wrap_content的時候,weight所代表的是你的控件要優先盡可能的大。

所以,要對weight做了解,要深深的理解下面兩句話:

在layout_width設置為fill_parent的時候,layout_weight所代表的是你的控件要優先盡可能的大,但這個大是有限度的,即fill_parent.

在layout_width設置為wrap_content的時候,layout_weight所代表的是你的控件要優先盡可能的小,但這個小是有限度的,即wrap_content.

layout_height 同 layout_width.

分享到:
评论

相关推荐

    androidlayout-marginBottom的值为负数.docx

    为什么有时候像android:layout_marginBottom等变量的赋值为负数? 例如如下代码:  android:orientation="vertical"  android:id="@id/Widget_2X4_frame"  android:layout_width="fill_parent"  android:...

    Android实训购物车页面

    android:layout_weight="0.09"> <TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/zongji" android:text="总计:"> </TextView> <EditText android:layout_...

    Android购物车代码

    android:layout_weight="0.09"> <TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/zongji" android:text="总计:"> </TextView> <EditText android:layout_...

    Android控件大全以及各布局空间的使用方式

    android:layout_width="fill_parent" android:layout_height="wrap_content" android:collapseColumns="1"> <TableRow android:id="@+id/TableRow01" android:layout_width="fill_parent" android:layout_height...

    login_interface

    android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="15dip" android:background="@drawable/background_login" android:padding="15dip" > android:id="@+...

    点按弹出按钮组件

    android:layout_width="fill_parent" android:layout_height="fill_parent" zhy:position="left_top" zhy:radius="130dp" > android:layout_width="wrap_content" android:layout_height="wrap_content" ...

    android_QQ_例子

    用Eclipse加载项目工程 ... android:layout_height="wrap_content" android:layout_weight="0.66" android:background="@drawable/blue_bg" android:orientation="vertical" > android:layout

    android中ratingbar的简单使用

    android:layout_width="fill_parent" android:layout_height="fill_parent" > android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> android:id=...

    android-autofittextview-master.zip_android_android textview_auto

    android:layout_width= match_parent android:layout_height= wrap_content > <Button android:layout_width= match_parent android:layout_height= wrap_content android:singleLine= true /&gt...

    Android 百分比布局

    android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="24px" android:layout_weight="1" android:orientation="horizontal"> android:id="@+id/tv_solid...

    [Android应用开发电子书]

    android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" > android:id="@+id/ImageView1" android:layout_width="wrap_...

    Layout_Gravity

    android:layout_gravity 属性详细解读

    SendSms.zip

    android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="fill_horizontal" > android:id="@+id/address" android:layout_width="fill_parent" android:layout_height=...

    ANDROID实验报告组件布局.pdf

    layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="right" android:text="@string/username" android:textStyle="bold" android:textColor="#000000"/> <EditText android:layout_...

    android左右滚动焦点图【改】

    android:layout_height="wrap_content" android:layout_width="fill_parent" android:orientation="vertical"> android:id="@+id/gallery" android:layout_width="fill_parent" android:layout_height=...

    Android_Layout_之_RelativeLayout_代码实现相对布局

    点对点 Android_Layout_之_RelativeLayout_代码实现相对布局 使用

    layout_gravity和gravity区别以及应用

    layout_gravity 在 线性布局 和 帧布局 中的应用 通过代码 直观的展示 layout_gravity 和 gravity 的区别

    android 右侧滑动菜单 主页滑动 窗帘效果 拉幕

    android:layout_width="wrap_content" android:layout_height="fill_parent"> <include layout="@layout/main_left" /> android:id="@+id/sliding_menu_view" android:layout_width="fill_parent" android...

    android自定义弹出框

    android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" > <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width=...

    android水平导航条的实现

    android:layout_width="fill_parent" android:layout_height="fill_parent" > android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:...

Global site tag (gtag.js) - Google Analytics