【Android】界面布局之 LinearLayout(线性布局)

  线性布局是程序中最常见的一种布局方式,线性布局可以分为水平线性布局和垂直线性布局两种,通过android:orientation属性可以设置线性布局的方向。下面是一个简单的线性布局的例子:
  <?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="wrap_content"
  >
  <EditText
  android:layout_width="fill_parent"
  android:layout_height="wrap_content" />
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="horizontal"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:gravity="right">
  <Button
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="确定"/>
  <Button
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="取消"/>
  </LinearLayout>
  </LinearLayout>
  对应的模拟器上的布局效果以及框架图如下所示:


  最外层布局为垂直线性布局,宽度为整个屏幕(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"?>
  <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"><!-同一层次的三个LinearLayout的layout_weight都设置为1,每一个都占据三分之一的空间-->
  <LinearLayout
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:layout_weight="1">
  <Button
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="左上按钮"
  android:layout_gravity="left" /><!-因为外层的LinearLayout定义为垂直布局,这里定义为在Layout里面靠左显示,所以就显示在左上角-->
  </LinearLayout>
  <LinearLayout
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:layout_weight="1">
  <Button
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="右上按钮"
  android:layout_gravity="right" />
  </LinearLayout>
  </LinearLayout>
  <!-中心的按钮-->
  <LinearLayout
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:layout_weight="1" <!-同一层次的三个LinearLayout的layout_weight都设置为1,每一个都占据三分之一的空间-->
  android:gravity="center">
  <Button
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="中心按钮" />
  </LinearLayout>
  <!-最下面的两个按钮-->
  <LinearLayout
  android:orientation="horizontal"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:layout_weight="1"><!-同一层次的三个LinearLayout的layout_weight都设置为1,每一个都占据三分之一的空间-->
  <LinearLayout android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:layout_weight="1"
  android:gravity="left|bottom"><!-定义其内部元素的对齐方式:左下角-->
  <Button
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="左下按钮" />
  </LinearLayout>
  <LinearLayout
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:layout_weight="1"
  android:gravity="right|bottom"><!-定义其内部元素的对齐方式:右下角-->
  <Button
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="右下按钮"/>
  </LinearLayout>
  </LinearLayout>
  </LinearLayout>
  对应的模拟器上的布局效果以及框架图如下所示:

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值