首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

线性格局 Linearlayout layout_weight属性

2012-09-29 
线性布局 Linearlayout layout_weight属性线性布局LinearLayout是最常用的布局之一,它可以把包含的子元素(

线性布局 Linearlayout layout_weight属性
线性布局LinearLayout是最常用的布局之一,它可以把包含的子元素(View)排列成一列或者一行,即垂直方向或者水平方向,默认是水平方向,方向可以通过setOrientation()方法设置,可以通过setGravity设置子元素的对齐方式,还可以通过子元素的weight属性设置子元素在LinearLayout中占的显示比重;
<?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="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"/>
       
        <TextView
            android:text="green"
            android:gravity="center_horizontal"
            android:background="#00aa00"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"/>
       
        <TextView
            android:text="blue"
            android:gravity="center_horizontal"
            android:background="#0000aa"
            android:layout_width="fill_parent"
            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="fill_parent"
            android:layout_weight="1"/>
       
        <TextView
            android:text="row two"
            android:textSize="15pt"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"/>
       
        <TextView
            android:text="row three"
            android:textSize="15pt"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"/>
       
        <TextView
            android:text="row four"
            android:textSize="15pt"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"/>
       
    </LinearLayout>
       
</LinearLayout>

子元素的layout_weight 属性,值越小,所占得比重越大,分为两种情况:
举例说明:
如果水平显示,子元素的layout_width属性值为fill_parent,则layout_weight属性值越小,占得显示比例越大,layout_width属性值为wrap_content,则layout_weight属性值越小,显示比例越小。
如果是垂直显示,则注意子元素的layout_height属性,情况和水平一样 1 楼 wangshiming88 2011-07-25   默认是vertical 垂直方向的 2 楼 LoveZhou 2011-07-25   wangshiming88 写道默认是vertical 垂直方向的
恩,谢谢,我写错了

热点排行