`

LinearLayout中android:layout_weight的作用

阅读更多
一直不明白android:layout_weight的作用,今天终于弄明白了。
转自:http://www.cnblogs.com/fly3q/archive/2010/03/24/1693977.html
layout_weight 用于给一个线性布局中的诸多视图的重要度赋值。
     所有的视图都有一个layout_weight值,默认为零,意思是需要显示
     多大的视图就占据多大的屏幕空 间。若赋一个高于零的值,则将父视
     图中的可用空间分割,分割大小具体取决于每一个视图的layout_weight
       值以及该值在当前屏幕布局的整体 layout_weight值和在其它视图屏幕布
     局的layout_weight值中所占的比率而定。
     举个例子:比如说我们在 水平方向上有一个文本标签和两个文本编辑元素。
    该文本标签并无指定layout_weight值,所以它将占据需要提供的最少空间。
    如果两个文本编辑元素每一个的layout_weight值都设置为1,则两者平分
    在父视图布局剩余的宽度(因为我们声明这两者的重要度相等)。如果两个 
   文本编辑元素其中第一个的layout_weight值设置为1,而第二个的设置为2,
   则剩余空间的三分之一分给第一个,三分之二分给第二个。
看例子:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
  <LinearLayout
      android:orientation="horizontal"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:layout_weight="1">
      <TextView
          android:text="redwwwwwww"
          android:gravity="center_horizontal"
          android:background="#aa0000"
          android:layout_width="wrap_content"
          android:layout_height="fill_parent"
          android:layout_weight="1"/>
      <TextView
          android:text="green"
          android:gravity="center_horizontal"
          android:background="#00aa00"
          android:layout_width="wrap_content"
          android:layout_height="fill_parent"
          android:layout_weight="2"/>
      <TextView
          android:text="blue"
          android:gravity="center_horizontal"
          android:background="#0000aa"
          android:layout_width="wrap_content"
          android:layout_height="fill_parent"
          android:layout_weight="3"/>
      <TextView
          android:text="yellow"
          android:gravity="center_horizontal"
          android:background="#aaaa00"
          android:layout_width="wrap_content"
          android:layout_height="fill_parent"
          android:layout_weight="4"/>
  </LinearLayout>
</LinearLayout>

附件为运行的效果:


更新于2012-10-29:
之前学得不够深,所以上面只是表面的解析,更深的可以读:http://www.chess-ix.com/2012/01/17/the-use-of-layout_weight-with-android-layouts/http://www.cnblogs.com/angeldevil/archive/2012/html04/08/2437747
  • 大小: 3.7 KB
分享到:
评论
9 楼 追求幸福 2012-10-29  
a455642158 写道
整体竖直方向是这个分配率 内部进去就相反了……表面现象解释。

嗯,是啊!学艺不精!在不断地改正:这里有更好的解释:http://www.cnblogs.com/angeldevil/archive/2012/04/08/2437747.html[/url]和[url]http://www.chess-ix.com/2012/01/17/the-use-of-layout_weight-with-android-layouts/
8 楼 a455642158 2012-08-29  
整体竖直方向是这个分配率 内部进去就相反了……表面现象解释。
7 楼 z576250965 2012-03-22  
既然和android:layout_width="wrap_content"有关,那android:weight不就可有可无了吗?
6 楼 追求幸福 2012-01-04  
sunjiesh 写道
为什么我得到的是相反的答案呢?我已经确保使TEXT中的内容相同,但是发现效果是反而是android:layout_weight数值大的反而占据的空间大呢?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

   <TextView
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="3"
            android:background="#aa0000"
            android:gravity="center_horizontal"
            android:text="XXXX" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:background="#00aa00"
            android:gravity="center_horizontal"
            android:text="XXXX" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:background="#0000aa"
            android:gravity="center_horizontal"
            android:text="XXXX" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="3"
            android:background="#aaaa00"
            android:gravity="center_horizontal"
            android:text="XXXX" />

</LinearLayout>




大哥你的答案是对的!
5 楼 sunjiesh 2012-01-02  
为什么我得到的是相反的答案呢?我已经确保使TEXT中的内容相同,但是发现效果是反而是android:layout_weight数值大的反而占据的空间大呢?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

   <TextView
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="3"
            android:background="#aa0000"
            android:gravity="center_horizontal"
            android:text="XXXX" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:background="#00aa00"
            android:gravity="center_horizontal"
            android:text="XXXX" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:background="#0000aa"
            android:gravity="center_horizontal"
            android:text="XXXX" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="3"
            android:background="#aaaa00"
            android:gravity="center_horizontal"
            android:text="XXXX" />

</LinearLayout>


4 楼 追求幸福 2011-07-04  
liuwanwei 写道
图中的水平宽度比率是4:3:2:1吗?

按道理分析是会是1:2:3:4,但在实际情况中要考虑到其他的因素,比如我那实例代码中的TextView的宽度会受其text的字数影响的,因为有android:layout_width="wrap_content"这一句话。所以你可以看到我附件里面的那张效果图就明白了。
3 楼 追求幸福 2011-07-04  
liang86liang 写道
跑跑试试,实践出真知么!!哈哈

这位老兄说得对,一定要自己试试才知道。
2 楼 liang86liang 2011-07-01  
跑跑试试,实践出真知么!!哈哈
1 楼 liuwanwei 2011-06-30  
图中的水平宽度比率是4:3:2:1吗?

相关推荐

    android_QQ_例子

    用Eclipse加载项目工程 &lt;LinearLayout xmlns:android=... android:layout_weight="0.66" android:background="@drawable/blue_bg" android:orientation="vertical" &gt; android:layout

    Android实训购物车页面

    -&lt;LinearLayout android:background="@drawable/aaa" android:weightSum="1" android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" xmlns:android=...

    Android购物车代码

    -&lt;LinearLayout android:background="@drawable/aaa" android:weightSum="1" android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" xmlns:android=...

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

    android:layout_weight="1" android:layout_height="wrap_content" android:text="行1列2" /&gt; &lt;TextView android:layout_width="wrap_content" android:layout_weight="1" android:layout_height="wrap_content...

    android水平导航条的实现

    我用的是HorizontalScrollView来实现水平条的滚动,按钮的背景宽度和图片的一致,以免被拉伸,main.xml的配置如下: &lt;LinearLayout xmlns:android=... android:layout_weight="1" &gt;

    WeChatSample

    android:layout_weight="1" android:id="@+id/vp" android:layout_width="match_parent" android:layout_height="0dp"&gt;&lt;/android.support.v4.view.ViewPager&gt; &lt;LinearLayout android:paddingTop="10dp" ...

    九宫格牌翻转游戏demo

    android:layout_weight="1" android:src="@drawable/pbg" android:scaleType="centerCrop"/&gt; android:id="@+id/m3" android:layout_margin= "2dp" android:layout_width="60dp" android:layout_height=...

    WebViewDownloadTest.zip

    android:layout_weight="10" android:text="http://www.imust.cn/info/1062/5273.htm" /&gt; android:id="@+id/bt_go" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_...

    Android App中的多个LinearLayout嵌套布局实例解析

    如果LinearLayout是最外面的一层,它是不会弹出layout_weight属性的, 换句话说最外层不能用layout_weight xml布局如下 &lt;LinearLayout xmlns:android=http://schemas.android.com/apk/res/android android:...

    SwipeListViewTest项目

    &lt;Button android:id="@+id/example_row_b_action_2" android:layout_width="0dp" android:layout_height="60dp" android:layout_gravity="center" android:layout_marginLeft="10dp" android:layout_weight="1" ...

    Android实现图片自动轮换

    package xatu.cn.androidtest_one; import android.content.ContentResolver; import android.content.Intent;... android:layout_weight="1" /&gt; &lt;/LinearLayout&gt; &lt;/android.support.constraint.ConstraintLayout&gt;

    Android中的android:layout_weight使用详解

    layout_weight的作用是设置子空间在LinearLayout的重要度(控件的大小比重)。layout_weight的值越低,则控件越重要,下面为大家介绍下具体的使用方法

    CircleIndicator-一个轻量级的viewpager指示器 ,类似于nexus5 启动器的效果.zip

     android:layout_weight="1"&gt;  &lt;android.support.v4.view.ViewPager  android:id="@ id/viewpager_default"  android:layout_width="match_parent"  android:layout_height="match_parent"/&gt;    ...

    高仿微信界面

    android:layout_weight="1"&gt; &lt;/android.support.v4.view.ViewPager&gt; android:layout_width="match_parent" android:layout_height="0.5dp" android:background="#737373" /&gt; android:layout_width=...

    wkp111_StickLayout-粘性控件,其任意一个子控件都可滑动停留,本质为NestedScrollView和LinearLayout的结合。.zip

     android:layout_weight="1"  android:layout_width="0dp"  android:layout_height="match_parent"/&gt;    android:onClick="scrollTo3"  android:background="@android:color/holo_green_light"  android:...

    Android layout_weight使用方法及实例

    &lt;LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”match_parent” android:layout_height=”match_parent” android:orientation=”horizontal”&gt; &lt;...

    LCRapidDevelop-master

    android:layout_weight="1" android:layout_height="wrap_content" android:layout_marginLeft="15dp" android:layout_marginTop="5dp" android:layout_marginBottom="5dp" android:layout_marginRight="10dp...

    目录选择器源代码

    android:id="@+id/dir_str" android:gravity="center" android:layout_weight="1" /&gt; --&gt; android:id="@+id/btn_back" android:layout_width="40dip" android:layout_height="40dip" android:text="BACK" ...

    android layout 按比例布局的代码

    为了创建比例大小的子View,可以将LinearLayout的宽度和...对于LinearLayout的几个子View,将它们的宽度都定义为0,android:layout_width=”0dip”,然后使用layout_weight 为每个View指定宽度比例,本例为每个TextVie

    Android布局之LinearLayout线性布局

    android:layout_weight—– 给控件分配剩余空间 先给大家展示一下导图: 知识点详解(演示效果方便组件没有设置id) (1)gravity和Layout_gravity android:gravity 属性是对该view中内容的限定.比如一个...

Global site tag (gtag.js) - Google Analytics