父视图剩余的空间分配给设 置了layout_weight的组件。这个属性可以让LinearLayout里不同的组件分配不同宽度/高度变得非常灵活。默认的 android:layout_weight = 0,即缺省。layout_weight的值越大,所占比例也越大。

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="horizontal"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    ><TextView      android:layout_width="wrap_content"     android:layout_height="fill_parent"     android:gravity="center_horizontal"    android:text="red"     android:layout_weight="1"    android:background="#aa0000"    />    <TextView      android:layout_width="wrap_content"     android:layout_height="fill_parent"     android:gravity="center_horizontal"    android:text="green"     android:layout_weight="1"    android:background="#00aa00"    />    <TextView      android:layout_width="wrap_content"     android:layout_height="fill_parent"     android:gravity="center_horizontal"    android:text="blue"     android:layout_weight="1"    android:background="#0000aa"    /><TextView      android:layout_width="wrap_content"     android:layout_height="fill_parent"     android:gravity="center_vertical"    android:text="yellow"     android:layout_weight="1"    android:background="#aaaa00"    /></LinearLayout>

这里4个TextView控件都带有 android:layout_weight属性,且值都是1,所以将子LinearLayout的空间平均分成了4份。

android:gravity属性的作用是设置组件与 组建or文字 之间的对齐方式,主要有

center_vertical、center_horizontal、center、top、等等值,上面的代码效果如图:

如果将TextView改为 android:layout_width="wrap_content" ,效果变为:

各颜色条的宽度是不一致的,因为red,green,blue和yellow这几个字符串的长度不一样。

如果想既保持字符串不一致,又想得到相同宽度的颜色条,可以把TextView中的属性android:layout_width值改为"fill_parent"

将代码中红色TextView的layout_weight修改为2,显示效果如下:

由于字符串长度不一致,所以这里并不是2:1:1:1的比例.

原文来自:

http://hi.baidu.com/hbzha/blog/item/8af2b44f9bd8bd1eb2de055b.html

更多相关文章

  1. android组建属性及使用许可
  2. 实现Launcher的抽屉效果
  3. Android读写XML(下)——创建XML文档
  4. TextView的XML属性说明全析 ---Android基础篇
  5. TextView属性大全
  6. android之TextView属性
  7. AndroidManifest.xml文件剖析 (二)
  8. android taskAffinity属性
  9. Android图形层叠 – Layer-list

随机推荐

  1. Android拒绝来电的实现--ITelephony类的
  2. android openGL ES 01
  3. Android(安卓)中导入Gson jar包后出现 ja
  4. 联系人数据库设计之PhotoPriorityResolve
  5. Android中运行时配置环境的改变
  6. Android(安卓)Gallery的使用
  7. android定时任务的实现
  8. android 多媒体部分学习笔记九----数字音
  9. Android(安卓)XML解析之PULL及单元测试
  10. 获取 Android(安卓)设备 屏幕,内存,存储大