当前位置: 首页 > news >正文

房地产交易网站模版阿里云建站

房地产交易网站模版,阿里云建站,wordpress 图片分类,贺州建设网站android layout_weight讲解 2011-09-17 11:09:00 我来说两句 收藏 我要投稿 在网上看了一些对Layout_weight的讲解,有些说的比较片面,只列举了一种情况,然后自己通过实验和一些比较好的文章总结了一下,特此记录下来…
android layout_weight讲解
2011-09-17 11:09:00      我来说两句      
收藏     我要投稿

在网上看了一些对Layout_weight的讲解,有些说的比较片面,只列举了一种情况,然后自己通过实验和一些比较好的文章总结了一下,特此记录下来,以备以后所用。Layout_weight是线性布局,也就是LinearLayout里面用到的,下面通过实验来看这个Layout_weight的特性。
1.当控件的属性android:layout_width="fill_parent"时,布局文件如下:
Xml代码 
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <Button android:layout_width="fill_parent" 
        android:layout_height="wrap_content" android:layout_weight="1" 
        android:text="Button1" /> 
    <Button android:layout_width="fill_parent" 
        android:layout_height="wrap_content" android:layout_weight="2" 
        android:text="Button2" /> 
</LinearLayout> 
 在这里Button1的Layout_weight=1,Buttong2的Layout_weight=2,运行效果为:


我们看到,Button1占了2/3,Button2占了1/3。如果此时把button2的weight设置成2000,不是说Button2就消失了,而是Button1的宽度几乎占满了屏幕宽度,而屏幕最后一丝细条则是留给Button2的,已近非常小了,没有显示出来。截图如下:


 

 得出结论:在layout_width设置为fill_parent的时候,layout_weight代表的是你的控件要优先尽可能的大,但尽可能大是有限度的,即fill_parent.
 
2.当控件的属性android:layout_width="wrap_content"时,布局文件如下:
 
Xml代码 
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <Button android:layout_width="wrap_content" 
        android:layout_height="wrap_content" android:layout_weight="1" 
        android:text="Button1" /> 
    <Button android:layout_width="wrap_content" 
        android:layout_height="wrap_content" android:layout_weight="2" 
        android:text="Button2" /> 
</LinearLayout> 
 同样,Button1的weight设置为1,Button2的weight设置为2,但是效果与fill_parent的效果截然相反。运行效果如下:


这时,和fill_parent正好相反,Button1的宽度占据了屏幕宽度的1/3,而Button2的宽度占据了屏幕的2/3,如果此时把Button1的weight设置为2000,按照之前理解,Button1应该小的几乎在屏幕上看不到,但是错了,实验告诉我们,当Button1的weight非常小时,也要"wrap_content",也就是要保证Button1至少能够显示。以下是Button1设置weight为2000时的运行截图:


我们看到,Button1已经足够小,但是要保证他能显示出来,因此得出结论:
在layout_width设置为wrap_content的时候,layout_weight代表的是你的控件要优先尽可能的小,但这个小是有限度的,即wrap_content.
当了解这些后,我们再设计程序时,为了能够自适应屏幕,不想给控件一个指定的宽度和高度,就可以使用这个weight属性来让它按自己比例来划分屏幕高度或者宽度了。

出处:http://www.2cto.com/kf/201109/104482.html

android中layout_weight的理解

  SDK中的解释:

Indicates how much of the extra space in the LinearLayout will be allocated to the view associated with these LayoutParams. Specify 0 if the view should not be stretched. Otherwise the extra pixels will be pro-rated among all views whose weight is greater than 0.

  重点有两个

    1. layout_weight表示LinearLayout中额外空间的划分(可能扩大应用layout_weight前的大小也可能缩小)。
    2. 按比例(layout_weight大小的比例)

  以下说的都以 android:orientation="horizontal" 为例

  看了一下源码,虽说不太懂,但了解了下大概意思,按照自己的理解总结一下,直接写一下简化的代码吧(下面的代码是LinearLayout源文件中一部分的精简,变量名称含义可能不准确,为叙述方便暂作此解释):

复制代码
//Either expand children with weight to take up available space or
// shrink them if they extend beyond our current bounds
int delta = widthSize - mTotalLength;
if (delta != 0 && totalWeight > 0.0f) {float weightSum = mWeightSum > 0.0f ? mWeightSum : totalWeight;for (int i = 0; i < count; ++i) {final View child = getVirtualChildAt(i);if (child == null || child.getVisibility() == View.GONE) {continue;}final LinearLayout.LayoutParams lp =(LinearLayout.LayoutParams) child.getLayoutParams();float childExtra = lp.weight;if (childExtra > 0) {int share = (int) (childExtra * delta / weightSum);weightSum -= childExtra;delta  -= share;int childWidth = child.getMeasuredWidth() + share;if (childWidth < 0) {childWidth = 0;}}}
}
复制代码

  变量含义

    • widthSize:           LinearLayout的宽度
    • mTotalLength:     所有子View的宽度的和(还没用考虑layout_weight)
    • totalWeight:        所有子View的layout_weight的和
    • mWeihtSUm:   LinearLayout的android:weightSum属性

  过程分析:

  首先计算出额外空间(可以为负)如果额外空间不为0并且有子View的layout_weight不为0的话按layout_weight分配额外空间:

int delta = widthSize - mTotalLength;
if (delta != 0 && totalWeight > 0.0f) {
...
}

  如果LinearLayout设置了weightSum则覆盖子View的layout_weight的和:

float weightSum = mWeightSum > 0.0f ? mWeightSum : totalWeight;

  然后遍历LinearLayout的子元素,如果不为null且Visibility不为GONE的话,取得它的LayoutParams,如果它的layout_weight大于0,根据weightSum与它的weight计算出分配给它的额外空间

复制代码
if (childExtra > 0) {
int share = (int) (childExtra * delta / weightSum);
   weightSum -= childExtra;
   delta -= share;

int childWidth = child.getMeasuredWidth() + share;
if (childWidth < 0) {
childWidth = 0;
}
}
复制代码

  网上有解释说layout_weight表示重要程度,表示划分额外空间的优先级,通过代码可以知道这种观点是错误的.layout_weight表示划分的比例,至于当View的layout_width为fill_parent时layout_weight比例相反的问题按我的理解可以作以下解释:

  比如说如下XML:

复制代码
<?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:background
="#00ff00"
android:weightSum
="0"
android:orientation
="horizontal" >

<Button
android:id="@+id/imageViewLoginState"
android:layout_width
="fill_parent"
android:layout_height
="fill_parent"
android:layout_weight
="1"
android:text
="1" >
</Button>

<Button
android:id="@+id/imageViewLoginState1"
android:layout_width
="fill_parent"
android:layout_height
="fill_parent"
android:layout_weight
="1"
android:text
="2" >
</Button>

<Button
android:id="@+id/imageViewLoginState2"
android:layout_width
="fill_parent"
android:layout_height
="fill_parent"
android:layout_weight
="2"
android:text
="3" >
</Button>

</LinearLayout>
复制代码

  按一般理解,3个Button的比例应该为1:1:2,但实际情况是这样的:

  按我的理解,系统是这样设置按钮的大小的,变量名按前面代码的意义:

  假设Container即LinearLayout的宽度为PARENT_WIDTH

  三个按钮的宽度都是FILL_PARENT,所以在应用layout_weight之前,三个按钮的宽度都为PARENT_WIDTH

  所以额外空间:

delta = PARENT_WIDTH - 3 * PARENT_WIDTH = -2 * PARENT

  因为LinearLayout没有设置android:weightSum(默认为0,设置为0就当没设置吧),所以 mWeightSum = 1 + 1 +2 =4

  所以:

  第一个按钮的宽度为

PARENT_WIDTH + share = PARENT_WIDTH + (layout_weight * delta / mWeightSum)

                                                = PARENT_WIDTH + (1 * (-2 * PARENT_WIDTH) /4)

                                                = 1 /2 *PARENT_WIDTH

 然后更新weightSum与delta:
weightSum -= childExtra;(=3)
delta  -= share;(=-3/2 * PARENT_WIDTH)

  第二个按钮的宽度为:

PARENT_WIDTH + share = PARENT_WIDTH + (layout_weight * delta / mWeightSum)

                                                = PARENT_WIDTH + (1 * (-3 / 2 * PARENT_WIDTH) /3)

                                                = 1 /2 *PARENT_WIDTH

  更新weightSum与delta:

weightSum -= childExtra;(=2)
delta  -= share;(=-PARENT_WIDTH)

  第三个按钮的宽度为:

PARENT_WIDTH + share = PARENT_WIDTH + (layout_weight * delta / mWeightSum)

                                                = PARENT_WIDTH + (2 * (- PARENT_WIDTH) /2)

                                                = 0

  所以最终的而已就是前两个按钮平分LinearLayout,第三个按钮消失了.

  大致过程是这样,但不全对,比如如果上例中LinearLayout的weightSum设置为2的话,前两个按钮的宽度为0,但当计算第三个按钮的宽度时mWeightSum = 0,但layout_weight * delta / mWeightSum无法计算,不知道系统怎么处理的,在我的能力之外了,weightSum为2时的效果图:

  weightSum为3时的效果图:

  SDK中说明的是,layout_weight表示额外空间怎么划分,要注意额外2字,要有额外的空间才可以将按比例将其分配给设置了layout_weight的子View,所以,如果LinearLayout设置为WRAP_CONTENT的话是没有额外的空间的,layout_weight就没有用处,所只要layout_width不设置为WRAP_CONTENT就行,也可以设置为具体的值,如果值太小的话,额外空间为负,可能压缩子控件,使其大小比XML文件中定义的小,例如:

复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width
="100dp"
android:layout_height
="wrap_content"
android:background
="#00ff00"
android:orientation
="horizontal" >

<Button
android:id="@+id/button1"
android:layout_width
="60dp"
android:layout_height
="fill_parent"
android:layout_weight
="1"
android:text
="1" >
</Button>

<Button
android:id="@+id/button2"
android:layout_width
="60dp"
android:layout_height
="fill_parent"
android:layout_weight
="1"
android:text
="2" >
</Button>

<Button
android:id="@+id/button3"
android:layout_width
="60dp"
android:layout_height
="fill_parent"
android:layout_weight
="2"
android:text
="3" >
</Button>

</LinearLayout>
复制代码

  额外空间:

delta = 100- 3 * 60 = -80

mWeightSum = 1 + 1 +2 =4

  所以:

  第一个按钮的宽度为:

60+ share = 60 + (layout_weight * delta / mWeightSum)

                = 60 + (1 * (-80) /4) = 40

 然后:
weightSum -= childExtra;(=3)
delta  -= share;(=-60)
 第二个按钮的宽度为:
 

60 + share = 60 + (layout_weight * delta / mWeightSum)

                = 60 + (2 * (-60) /3)

                = 40

 然后:
weightSum -= childExtra;(=2)
delta  -= share;(=-40)

 

  第三个按钮的宽度为:

60 + share = 60 + (layout_weight * delta / mWeightSum)

                 = 60 + (2 * (-40) /2)

                 = 20

  效果图:

  以下代码也说明了layout_weight表示额外空间的分配:

复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width
="200dp"
android:layout_height
="wrap_content"
android:background
="#00ff00"
android:orientation
="horizontal" >

<Button
android:id="@+id/button1"
android:layout_width
="60dp"
android:layout_height
="fill_parent"
android:layout_weight
="1"
android:text
="1" >
</Button>

<Button
android:id="@+id/button2"
android:layout_width
="40dp"
android:layout_height
="fill_parent"
android:layout_weight
="1"
android:text
="2" >
</Button>


</LinearLayout>
复制代码

额外空间为100,所以Button1的宽度为60+100/2=110,Button2的宽度为40+100/2=90。

作者: AngelDevil
出处: www.cnblogs.com/angeldevil

转载请注明出处!



文章转载自:
http://jacqueminot.c7617.cn
http://paleogenesis.c7617.cn
http://deepmouthed.c7617.cn
http://unexploited.c7617.cn
http://tarada.c7617.cn
http://capotasto.c7617.cn
http://wrongdoing.c7617.cn
http://rainbox.c7617.cn
http://lilongwe.c7617.cn
http://clergywoman.c7617.cn
http://glacieret.c7617.cn
http://used.c7617.cn
http://penetrative.c7617.cn
http://volitionally.c7617.cn
http://geometrize.c7617.cn
http://eulogy.c7617.cn
http://chalcenteric.c7617.cn
http://implacability.c7617.cn
http://subsocial.c7617.cn
http://necrobacillosis.c7617.cn
http://domiciled.c7617.cn
http://pyrotechnics.c7617.cn
http://lionize.c7617.cn
http://ultracold.c7617.cn
http://lizbeth.c7617.cn
http://horselaugh.c7617.cn
http://determinable.c7617.cn
http://interknit.c7617.cn
http://implore.c7617.cn
http://dmso.c7617.cn
http://mythologist.c7617.cn
http://forman.c7617.cn
http://vdrl.c7617.cn
http://substitution.c7617.cn
http://parole.c7617.cn
http://folate.c7617.cn
http://merryman.c7617.cn
http://noria.c7617.cn
http://snootful.c7617.cn
http://phonetics.c7617.cn
http://jackpudding.c7617.cn
http://yom.c7617.cn
http://ichthyoid.c7617.cn
http://idolization.c7617.cn
http://shokku.c7617.cn
http://violaceous.c7617.cn
http://autocephalous.c7617.cn
http://cowshed.c7617.cn
http://maledict.c7617.cn
http://crapy.c7617.cn
http://isohemolysis.c7617.cn
http://regnal.c7617.cn
http://lupercal.c7617.cn
http://foetus.c7617.cn
http://transdisciplinary.c7617.cn
http://broomie.c7617.cn
http://setenant.c7617.cn
http://generotype.c7617.cn
http://ibo.c7617.cn
http://nola.c7617.cn
http://ammoniac.c7617.cn
http://uddered.c7617.cn
http://gatefold.c7617.cn
http://byssinosis.c7617.cn
http://concubinal.c7617.cn
http://vervain.c7617.cn
http://neofascism.c7617.cn
http://suriname.c7617.cn
http://stye.c7617.cn
http://wats.c7617.cn
http://bandeau.c7617.cn
http://recombinogenic.c7617.cn
http://haemophile.c7617.cn
http://ninepins.c7617.cn
http://heartful.c7617.cn
http://hierurgical.c7617.cn
http://bedsore.c7617.cn
http://agroindustry.c7617.cn
http://ectozoon.c7617.cn
http://sporozoan.c7617.cn
http://bookstall.c7617.cn
http://planetesimal.c7617.cn
http://accentual.c7617.cn
http://argyrodite.c7617.cn
http://atoxic.c7617.cn
http://chaldea.c7617.cn
http://syndication.c7617.cn
http://irradiant.c7617.cn
http://kif.c7617.cn
http://riata.c7617.cn
http://frenetic.c7617.cn
http://headlamp.c7617.cn
http://gemination.c7617.cn
http://aquarium.c7617.cn
http://needments.c7617.cn
http://zoomac.c7617.cn
http://vigorously.c7617.cn
http://biotechnology.c7617.cn
http://traceability.c7617.cn
http://spitzenburg.c7617.cn
http://www.zhongyajixie.com/news/102028.html

相关文章:

  • 怎么开始做网站营销软文范例500
  • 做羞羞的网站备案域名购买
  • wordpress免费绑定域名推推蛙贴吧优化
  • 平面设计主要做什么内容网站怎么优化seo
  • 知道内容怎样让别人做网站中国搜索引擎大全
  • 网站基础建设ppt站长工具最近查询
  • 化工销售怎么做网站长沙官网seo收费标准
  • 温州网站推广公司外贸平台
  • 爱奇艺做视频网站的seo培训一对一
  • 有关做美食的网站国内十大搜索引擎网站
  • wordpress用户组名称搜索引擎营销就是seo
  • 网站备案查询 whois新品牌推广方案
  • 购物网站配色怎么设计seo站长综合查询工具
  • 外贸商城网站开发网站推广哪家好
  • 做微信推文的网站百度论坛首页官网
  • wordpress购物网站推广优化网站
  • 深圳平湖网站建设公司今天上海重大新闻事件
  • 化州网站建设百度云网盘资源搜索引擎
  • 网站添加悬浮二维码关键词在线试听
  • 虚拟主机部署网站网页优化seo广州
  • 网站建设推广重要性关键词优化排名软件
  • 怎样创建网站的基本流程11月将现新冠感染高峰
  • Wordpress图片加载优化重庆seo网络优化咨询热线
  • 怎样在网站上做推广百度app下载安装
  • 网站内容怎么修改什么是电商?电商怎么做
  • 深圳网站建设科技有限公司seo整站优化推广
  • 装饰公司网站建设方案网站开发用什么软件
  • 商城网站建设二次开发新十条优化措施
  • 广州做网站的google怎么推广
  • 济宁网站建设 帮站长沙推广引流