线性布局是程序中最常见的一种布局方式,线性布局可以分为水平线性布局和垂直线性布局两种,通过android:orientation属性可以设置线性布局的方向。下面是一个简单的线性布局的例子:
  <?xml version="1.0" encoding="utf-8"?>
    android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  >
    android:layout_width="fill_parent"
  android:layout_height="wrap_content" />
    android:orientation="horizontal"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:gravity="right">
    android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="确定"/>
    android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="取消"/>
  

  

  对应的模拟器上的布局效果以及框架图如下所示:


  最外层布局为垂直线性布局,宽度为整个屏幕(fill_parent),高度为刚好适合子控件(wrap_content)。然后依次添加一个EditText,一个水平布局的LinearLayout,在这个线性布局里面,摆放两个Button,该线性布局的gravity属性设置为”right”,所以里面的两个Button靠右显示。
  1、android:gravity
  LinearLayout标签有一个非常重要的属性gravity,该属性用于控制布局中控件的对齐方式。如果是没有子控件的控件设置此属性,表示其内容的对齐方式,比如说TextView里面文字的对齐方式;若是有子控件的控件设置此属性,则表示其子控件的对齐方式,比如上面例子里面的两个Button的对齐方式。gravity可以取以下值,如果需要设置多个属性值,需要使用“|”进行组合:


  android:gravity与android:layout_gravity的区别:android:gravity定义了这个元素内所有子元素对于这个元素的布局,比如一个TextView内部文字的对齐方式;android:layout_gravity定义了这个元素相对于父元素(比如Layout)的布局,比如一个TextView在整个Layout中的位置。这两个属性对应的常量值都是一样的,如上表所示。
  2、android: layout_weight
  首先需要注意的是,没有android:weight这样的属性,这个与上面的android:gravity不一样。我们可以通过设置控件的layout_weight属性以控制各个控件在布局中的相对大小。layout_weight属性是一个非负整数值。线性布局会根据该控件layout_weight值与其所处布局中所有控件layout_weight值之和的比值为该控件分配占用的区域。例如,在水平布局的LinearLayout中有两个Button,这两个Button的layout_weight属性值都为1,那么这两个按钮都会被拉伸到整个屏幕宽度的一半。如果layout_weight指为0,控件会按原大小显示,不会被拉伸;对于其余layout_weight属性值大于0的控件,系统将会减去layout_weight属性值为0的控件的宽度或者高度,再用剩余的宽度或高度按相应的比例来分配每一个控件显示的宽度或高度。下例简要说明该属性的用法:
  <?xml version="1.0" encoding="utf-8"?>
    android:orientation="vertical" android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  
    android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:layout_weight="1">
    android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:layout_weight="1">
    android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="左上按钮"
  android:layout_gravity="left" />
  

    android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:layout_weight="1">
    android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="右上按钮"
  android:layout_gravity="right" />
  

  
  
    android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:layout_weight="1"
  android:gravity="center">
    android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="中心按钮" />
  
  
    android:orientation="horizontal"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:layout_weight="1">
    android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:layout_weight="1"
  android:gravity="left|bottom">
    android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="左下按钮" />
  

    android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:layout_weight="1"
  android:gravity="right|bottom">
    android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="右下按钮"/>
  
  
  
  对应的模拟器上的布局效果以及框架图如下所示:

更多相关文章

  1. Android中ImageView的scaleType 属性说明。
  2. Android那点事-系列之(一)Parcelable和Serializable的区别与使用
  3. Android(安卓)之数据的传递与传回
  4. Android(安卓)Study 之 初识ButterKnife(8.5.1)及简单运用
  5. Android逆向破解之Mointor查看目标日志
  6. Android(安卓)ScrollView嵌套WebView出现大面积空白页解决方法
  7. [置顶] Android自定义View入门
  8. Android常用需求二:应用字体字号切换及简单适配.
  9. Android四大布局及属性

随机推荐

  1. Android(安卓)游戏框架 libgdx 学习笔记
  2. davlik虚拟机内存管理之一——内存分配
  3. Android(安卓)开源项目第三篇——优秀项
  4. Android(安卓)framework add service
  5. Android(安卓)菜鸟历程5
  6. 个性化TextView内容
  7. Android实现传感器应用及位置服务
  8. Android(安卓)Volloy 网络请求框架图
  9. Android开发者指南(9) —— ProGuard
  10. 关于greenDAO,看这篇文章就够了