线性布局

概要:线性布局是将放入其中的组件按照垂直或水平方向来布局,也就是控制放入其中的组件横向或纵向排列。在线性布局中,每一行(针对垂直排列)或每一列(针对水平排列)中只能放一个组件,并且Android的线性布局不会换行,当组件排列当窗体的边缘时,后面的组件将不会被显示出来。

基本的语法格式:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/ic_launcher_background"
>

<Button android:text="按钮1" android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
<Button android:text="按钮2" android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
<Button android:text="按钮3" android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
<Button android:text="按钮4" android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
</LinearLayout>
常见的属性包括android:orientation,android:gravity,android:layout_width,android:layout_height,android:id和android:background,
其中前两个是线性布局管理器支持的属性,后面四个是android.view.View和android.view.ViewGroup支持的属性
,下面进行详细的介绍。
1,android:orientation属性
这个属性是用来设置布局管理器内组件的排列方式,其可选值为horizontal和vertical,默认值为vertical。其中,horizontal表示水平排列,vertical表示垂直排列。
2,Android:gravity属性
这个属性用于设置布局管理器内的组件对齐方式,其可选值包括

       top:不改变大小,位置置于容器的顶部

        bottom:不改变大小,位置置于容器的底部

        left:不改变大小,位置置于容器的左边

        right:不改变大小,位置置于容器的右边

        center_vertical:不改变大小,位置置于容器的纵向中央部分

        center_horizontal:不改变大小,位置置于容器的横向中央部分

        center:不改变大小,位置置于容器的横向和纵向的中央部分

        fill_vertical:可能的话,纵向延伸可以填满容器

        fiil_horizontal:可能的话,横向延伸可以填满容器

        fiil:可能的话,纵向和横向延伸填满容器

3.Android:layout_width属性
这个属性用来设置组件的基本宽度,其可选值包括fill_parent.match_parent和wrap_content。其中fill_parent表示该组件的宽度与父容器的宽度相同,match_parent和fill_parent作用完全相同,wrap_content表示该组件的宽度恰好能包裹他的内容。
4,android:layout_height属性
这个属性用来设置组件的基本高度,与3类似;
5Android:id属性
这个属性用于为当前组件指定一个id属性,在Java代码中可以应用该属性单独引用这个组件。为组件指定id属性后,在R.java文件中,会自动派生一个对应的属性,在java代码中,可以通过findViewById()方法来获取它。
6Android:background属性
这个属性用于为组件设置背景,可以是背景图片,也可以是背景颜色。为组件指定背景图片时 ,可以将准备好的的背景图片复制到目录下,然后使用下面的代码进行设置:
android:bakground="#ffffffff"

猜你喜欢

转载自www.cnblogs.com/gb12138/p/9633335.html
今日推荐