【Android学习】android:layout_weight的用法实例

对于android:layout_weight的用法,用下面的例子来说明:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.chenming.test.MainActivity" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:background="#ff0000">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#00ff00"
            android:textSize="35sp"
            android:text="first"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#0000ff"
            android:textSize="35sp"
            android:text="second"/>
    </LinearLayout>
      
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:background="#ff0000">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#00ff00"
            android:textSize="35sp"
            android:layout_weight="1"
            android:text="first"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#0000ff"
            android:layout_weight="1"
            android:textSize="35sp"
            android:text="second"/>
    </LinearLayout>  

        <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:background="#ff0000">
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="#00ff00"
            android:textSize="35sp"
            android:layout_weight="1"
            android:text="first"/>
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="#0000ff"
            android:layout_weight="1"
            android:textSize="35sp"
            android:text="second"/>
    </LinearLayout>  
    
</LinearLayout>

 以上是activity_main.xml布局文件,其效果如下:

对应刚好三行三个例子,由XML文件可以看到,红色表示空白区域,绿色表示第一个控件,蓝色表示第二个控件。

例子一:为正常的顺序排列,他们的 android:layout_width都是包含内容,所以长度为蓝色比较长,剩下的为空白区域。

例子二:在上一个例子的基础上,加入了分别加入android:layout_weight="1",weight的中文意思是权重,那么很明显,这个意思是两个控件的权重值1:1,但是要注意,这个不是说两个控件的长度是1:1,而是说在已有的长度下,把剩余的空白空间按照1:1再分给两个空间。所以,所谓的1:1指下图中的两条黑线是1:1,实际是蓝色控件长度大于绿色控件(因为second的英文长度比first长)。

例子三:那么,要如何能让蓝色控件长度等于绿色控件?只需让一开始的未分配1:1空白空间时候长度相等即可,所以例子三是在例子二的基础上把android:layout_width="wrap_content"改为android:layout_width="0dp",即宽度都为0而不是刚好包含内容,这样一来,剩余空白空间就是一整行,再按1:1分配就能是使得两个控件长度相等,也就是第三行例子三的样子。

 

所以android:layout_weight的用法要记住是分配剩余空白空间,而不是一开始的空间!后面改成2:3,4:5等等也都一样的道理。

 

posted @ 2016-08-23 21:40  日月星陈  阅读(268)  评论(0编辑  收藏  举报