Android学习笔记十三之LinearLayout布局

来源:互联网 发布:第二人称的小说知乎 编辑:程序博客网 时间:2024/06/04 19:24

Android学习笔记十三之LinearLayout布局

View和ViewGroup

  在Android应用中,所有的界面元素都是由View和ViewGroup构成的,View是一个用于绘制与用户交互的对象,ViewGroup是一个存放View对象的布局容器。android系统为我们提供了许多View和ViewGroup对象的子类的集合,我们可以使用这些对象,也可以基于View自定义我们需要的组件。

  这一节我们介绍LinearLayout线性布局,线性布局中,所有的组件会线性排列,有两个方向,横向或者纵向,用orientation这个属性控制。下面介绍一下LinearLayout的常用属性:

LinearLayout的常用属性有:

  • android:orientation属性:控制LinearLayout的排列方向,有两个值,分别是horizontal横向排列;vertical纵向排列
  • android:gravity属性:控制布局中控件的对齐方式,如果子控件中设置这个属性,则是控制子控件的内容的对齐方式。有如下值:buttom、left、right、center_vertical、fill_vertical、center_horizontal、fill_horizontal、center、fill、clip_vertical、clip_horizontal,如果需要同时设置多个属性,则需要用“|”分隔。例如“left|center”
  • android:layout_width属性:控件的宽度
  • android:layout_height属性:控件的高度
  • android:layout_weight属性:控制各个控件在布局中的相对大小,在常用于屏幕的适配,最简单的用法就是按比例划分。
  • android:divider属性:为LinearLayout设置分割线的图片,可以是颜色。
  • android:showDividers属性:设置分隔线的位置,有四个值:beginning、end、middle、none。
  • android:dividerPadding属性:设置分割线的padding。

这些是常用的属性,还有一个是android:layout_gravity,这个属性与android:gravity属性作用基本一样,但是也有区别:android:gravity是指定本元素的子元素相对它的对齐方式。android:layout_gravity是指定本元素相对它的父元素的对齐方式。

下面是几个LinearLayout的例子代码:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:showDividers="beginning"><LinearLayout    android:layout_width="match_parent"    android:layout_height="200dp"    android:orientation="horizontal">    <TextView        android:layout_width="0dp"        android:layout_height="match_parent"        android:layout_weight="1"        android:background="#ff0000"        android:gravity="center"        android:text="第一个"        android:textSize="18sp" />    <TextView        android:layout_width="0dp"        android:layout_height="match_parent"        android:layout_weight="1"        android:background="#00ff00"        android:gravity="center"        android:text="第二个"        android:textSize="18sp" /></LinearLayout><LinearLayout    android:layout_width="match_parent"    android:layout_height="200dp"    android:orientation="horizontal">    <TextView        android:layout_width="0dp"        android:layout_height="match_parent"        android:layout_weight="2"        android:background="#0000ff"        android:gravity="center"        android:text="第一个"        android:textSize="18sp" />    <TextView        android:layout_width="0dp"        android:layout_height="match_parent"        android:layout_weight="1"        android:background="#ff0000"        android:gravity="center"        android:text="第二个"        android:textSize="18sp" />    <TextView        android:layout_width="0dp"        android:layout_height="match_parent"        android:layout_weight="1"        android:background="#00ffff"        android:gravity="center"        android:text="第三个"        android:textSize="18sp" /></LinearLayout><LinearLayout    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:layout_marginTop="10dp"    android:orientation="vertical"    android:showDividers="beginning">    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:background="#00ffff"        android:gravity="center"        android:text="这是文本"        android:textSize="18sp" />    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginTop="10dp"        android:background="#ffff00"        android:gravity="center"        android:text="这是文本"        android:textSize="18sp" />    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginTop="10dp"        android:background="#ff0000"        android:gravity="center"        android:text="这是文本"        android:textSize="18sp" /></LinearLayout></LinearLayout>

效果图:

附上线性布局的国内镜像API和写的Demo

这里是简单的嵌套实现,利用嵌套可以实现很复杂的布局,关于LinearLayout就介绍到这里,下面就介绍到RelativeLayout。

原创粉丝点击