线性布局LinearLayout layout_weight属性(转)
2011-03-13 20:58
405 查看
转自http://lovezhou.javaeye.com/blog/827881
线性布局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属性,情况和水平一样
线性布局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属性,情况和水平一样
相关文章推荐
- Android:深度探究线性布局LinearLayout中权重(layout_weight)属性
- Android LinearLayout线性布局属性
- LinearLayout(线性布局)中weight的难点
- 线性布局(LinearLayout)下layout_weight用法分析
- Android LinearLayout布局的layout_weight属性探究
- android之线性布局LinearLayout以及weight权重使用
- Android中你也许不知道的线性布局Layout_weight属性权重比例分配原理
- Android布局之LinearLayout weight属性的理解
- linearlayout布局的属性 gravity layout_gravity layout_weight
- android之线性布局LinearLayout以及weight权重使用
- android之线性布局LinearLayout以及weight权重使用
- Android线性布局LinearLayout,及盒模型,weight权值,ScrollView和HorizontalScrollView【内容溢出(内容太多,高度/宽度大于能显示的最大高度/宽度)】
- 线性布局中的layout_weight属性
- 线性布局LinearLayout和相对布局RelativeLayout 比较
- 安卓布局 - 线性布局(LinearLayout)
- 【Android】利用相对布局布置更新软件的style为主题对话框的Activity,利用layout_weight属性对表格布局的行划分
- 安卓布局基础笔记(LinearLayout -- 线性布局 )
- Android布局的layout_weight属性
- 线性布局LinearLayout和相对布局RelativeLayout 比较
- LinearLayout中的layout_weight属性