Android-布局管理-线性布局

    线性布局是将放入其中的组件按照垂直水平方向来布局。每一行或每一列中只能放一个组件,而且Android的线性布局不会换行,当组件排列到窗口边缘后将不会被显示出来。基本的语法格式以下:java

<LinearLayout xmlns:android = "http://scshemas.android.com/apk/res/android"
   属性列表
   >
</LinearLayout>

    在线性布局管理器中,经常使用的属性包括android:orientation、android:gravity、android:layout_width、android:layout_height、android:idandroid:background。其中,前两个属性是属于线性布局管理器支持的属性,后面四个是android.view.View和android.view.ViewGroup支持的属性,下面进行详细介绍。android

一、android:orientation

    android:orientation属性用于设置布局管理器内组件的排列方式,其可选值为horizontalvertical,默认值为vertical。bash

  • horizontal  表示水平排序;
  • vertical       表示垂直排列;

二、android:gravity

    android:gravity属性用户设置布局管理器内组件的对齐方式,其可选值包括以下:布局

top 将对象放在其容器的顶部,不改变其大小.
bottom 将对象放在其容器的底部,不改变其大小.
left 将对象放在其容器的左侧,不改变其大小.
right 将对象放在其容器的右侧,不改变其大小.
center_vertical 将对象纵向居中,不改变其大小. 
垂直对齐方式:垂直方向上居中对齐。
fill_vertical 必要的时候增长对象的纵向大小,以彻底充满其容器. 
垂直方向填充
center_horizontal 将对象横向居中,不改变其大小. 
水平对齐方式:水平方向上居中对齐
fill_horizontal 必要的时候增长对象的横向大小,以彻底充满其容器. 
水平方向填充
center 将对象横纵居中,不改变其大小.
fill 必要的时候增长对象的横纵向大小,以彻底充满其容器.
clip_vertical

附加选项,用于按照容器的边来剪切对象的顶部和/或底部的内容. 剪切基于其纵向对齐设置:顶部对齐时,剪切底部;底部对齐时剪切顶部;除此以外剪切顶部和底部.spa

垂直方向裁剪code

clip_horizontal

附加选项,用于按照容器的边来剪切对象的左侧和/或右侧的内容. 剪切基于其横向对齐设置:左侧对齐时,剪切右侧;右侧对齐时剪切左侧;除此以外剪切左侧和右侧.xml

水平方向裁剪对象

    这些属性值也能够同时指定,各属性值之间用竖线隔开。例如,要指定组件靠右下角对齐,可使用属性值right|bottom排序

:Android:layout_gravity和android:gravity的使用区别;图片

三、android:layout_width

    本属性用于设置组件的基本宽度,其可选值包括以下:

  • fill_parent            表示该组件的宽度与父类容器的宽度相同;
  • match_parent      与fill_parent相同,从Android2.2开始推荐使用;
  • wrap_content      表示该组件的宽度刚好能包裹它的内容;
说明:android:layout_width属性是ViewGroup.LayoutParams所支持的XML属性,对于其余的布局管理器一样适用

注:android:layout_width和android:layout_weight关系

      android:layout_width和android:width的区别

四、android:layout_height

    本属性用于设置组件的基本高度,其可选值包括以下:

  • fill_parent            表示该组件的宽度与父类容器的宽度相同;
  • match_parent      与fill_parent相同,从Android2.2开始推荐使用;
  • wrap_content      表示该组件的宽度刚好能包裹它的内容;
说明:android:layout_width属性是ViewGroup.LayoutParams所支持的XML属性,对于其余的布局管理器一样适用

注:android:layout_height和android:height的区别

五、android:id

    本属性用于为当前组件指定一个id属性,在Java代码中能够应用该属性单独引用这个组件。为组件指定id属性后,在R.java文件中,会自动派生一个对应的属性,在Java代码中,能够经过findViewById()方法来获取它。

六、android:background

    本属性用于为组件设置背景,能够是背景图片,也能够是背景颜色。为组件指定背景图片时,能够将准备好的背景图片复制到目录下,而后使用下面的代码进行设置:

android:background="@drawable/background"

若是想指定背景颜色,可使用颜色值。代码以下:

android:background="#FFFFFFFF"

七、样例代码清单

  • 垂直列表
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/buttonCeshi1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="登陆1"/>
    <Button
        android:id="@+id/buttonCeshi2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="登陆2"/>
    <Button
        android:id="@+id/buttonCeshi3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="登陆3"/>
    <Button
        android:id="@+id/buttonCeshi4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="登陆4"/>
    <Button
        android:id="@+id/buttonCeshi5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="登陆5"/>

</LinearLayout>

效果以下:

  • 水平列表
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/buttonCeshi1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="登陆1"/>
    <Button
        android:id="@+id/buttonCeshi2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="登陆2"/>
    <Button
        android:id="@+id/buttonCeshi3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="登陆3"/>
    <Button
        android:id="@+id/buttonCeshi4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="登陆4"/>

</LinearLayout>

效果以下:

相关文章
相关标签/搜索