在google的教程里有如下的代码: 其中有几个标签重复指定了android:layout_weight属性,如:TextView 分别指定了“fill_parent”和“1”,这个是什么意思,想不明白,请知道的解答一下。<?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="red"
          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="1"/>
      <TextView
          android:text="blue"
          android:gravity="center_horizontal"
          android:background="#0000aa"
          android:layout_width="wrap_content"
          android:layout_height="fill_parent"
          android:layout_weight="1"/>
      <TextView
          android:text="yellow"
          android:gravity="center_horizontal"
          android:background="#aaaa00"
          android:layout_width="wrap_content"
          android:layout_height="fill_parent"
          android:layout_weight="1"/>
  </LinearLayout>
        
  <LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1">
    <TextView
        android:text="row one"
        android:textSize="15pt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>
    <TextView
        android:text="row two"
        android:textSize="15pt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>
    <TextView
        android:text="row three"
        android:textSize="15pt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>
    <TextView
        android:text="row four"
        android:textSize="15pt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>
  </LinearLayout>
  </LinearLayout>

解决方案 »

  1.   

    这个和android:weightSum有关哇,怎么回事呢。
      

  2.   

    不是重复定义,对象不是同一个。
    设置一个构件的布局为fill_parent将强制性地使构件扩展,以填充布局单元内尽可能多的空间。
    设置一个顶部布局或控件为fill_parent将强制性让它布满整个屏幕。 
    设置一个视图的尺寸为wrap_content将强制性地使视图扩展以显示全部内容。
      

  3.   

    ?????
            android:text="row three"
            android:textSize="15pt"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>
    中的android:layout_weight 不是有多个么?
      

  4.   

    <?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="fill_parent"
        android:orientation="horizontal" >
    <Button
        android:id="@+id/btn01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="wewew"
        android:layout_weight="1"
        />
    <Button 
        android:id="@+id/btn02"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="wewew"  
        android:layout_weight="1"   
        />
    <Button 
        android:id="@+id/btn03"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="wewew"
        android:layout_weight="1"    
        />
    <Button 
        android:id="@+id/btn04"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="wewew"
        android:layout_weight="1"    
        />
    </LinearLayout>
    比如上面的布局。button的父亲是Linearlayout,子类是水平排列的,每个button里面都有android:layout_weight="1"
    它的意思就是这四个button按照相同的比例占满父类的宽度。你可以试试。如果Linearlayout的orientation是垂直的,理解都是一样的。 要延伸的话,就是,android:layout_weight="x" ,x越大,所占的比例就越小。
      

  5.   

    layout_weight值来划分剩余的空间和其它Views定义的layout_weight也按比例进行空间的划分。
    如果每个文本框view的layout_weight 都被设置为1, 在父布局中的剩余的宽度将被它们平分.
    如果一个文本view的layout_weight值为2,另外一个是1, 那么剩余空间的三分之一将给第一个文本框,三分之二将给第二个文本框.
      

  6.   

    layout_weight值,默认为零,意思是需要显示 
         多大的视图就占据多大的屏幕空 间。若赋一个高于零的值,则将父视 
         图中的可用空间分割,分割大小具体取决于每一个视图的layout_weight 
           值以及该值在当前屏幕布局的整体 layout_weight值和在其它视图屏幕布 
         局的layout_weight值中所占的比率而定
      

  7.   

    就是固定长度按比例划分,我通常配合layout_width=“0”  使用 ,不这么用不行,不是光划分就行的,还会根据控件的大小出现问题,直接把宽度设为0就OK了,  LZ可以去看看linearlayout的源码,^_^