android学习笔记(二)布局

布局就是指android中如何摆放组件,android主要使用的布局有线性布局,绝对布局,相对布局,表格布局,框架布局这几种形式,下面首先来介绍一下,线性布局

一、线性布局
对于线性布局来说,就是指这个组件会在实际摆放时单独占用一行
具体摆放形式如下,关键字是linearlayout,以这个关键字开头就是线性布局,此布局下各组件会单独占用一行的空间,共有两种形式,水平和垂直,定义水平还是垂直需要用到android:orientation=”horizontal”关键字,horizontal是垂直,vertical即为水平
水平代码如下

<LinearLayout xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"<!-- 水平-->
    tools:context="com.example.helloandroid.SecondActivity" xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView
        android:id="@id/textViewSet1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:text="I am Second" />

    <CheckBox
        android:id="@+id/checkBox1"
        android:gravity="center_vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@id/textViewSet1"
        android:layout_alignParentTop="true"
        android:text="CheckBox" />

    <Button
        android:id="@+id/buttonclose1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:text="Button" />

</LinearLayout>

垂直代码如下

<LinearLayout xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    tools:context="com.example.helloandroid.SecondActivity" xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView
        android:id="@id/textViewSet1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="I am Second" />

    <CheckBox
        android:id="@+id/checkBox1"
        android:gravity="center_horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@id/textViewSet1"
        android:layout_alignParentTop="true"
        android:text="CheckBox" />

    <Button
        android:id="@+id/buttonclose1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="Button" />

</LinearLayout>

二、绝对布局
和名称一样,绝对布局就是通过设置每个组件的x,y来进行摆放的布局,关键字为AbsouluteLayout,需要设置的两个属性为android:layout_x和android:layout_y

示例代码:

<AbsoluteLayout xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context="com.example.helloandroid.SecondActivity" xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView
        android:id="@id/textViewSet1"
        android:layout_x="32pt"
        android:layout_y="32pt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="I am Second" />

    <Button
        android:id="@+id/buttonclose1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="76dp"
        android:layout_y="4dp"
        android:gravity="center_horizontal"
        android:text="Button" />

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="63dp"
        android:layout_y="114dp"
        android:text="CheckBox" />

</AbsoluteLayout>

三.相对布局
相对布局就是指其布局依赖于各组件,位置排放与参照的组件位置有关,相对布局的关键字为RelativeLayout,使用这个关键字后就代表此Activity的布局是相对布局,相对布局就需要设置如下属性
android:layout_below=”组件id”在该组件下方
android:layout_above=”组件id”在该组件上方
android:layout_toRightOf=”组件id”在该组件右方
android:layout_toLeftOf=”组件id”在该组件左方
参照代码:

<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context="com.example.helloandroid.SecondActivity" xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView
        android:id="@id/textViewSet1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="I am Second" />

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_below="@+id/buttonclose1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="CheckBox" />

    <Button
        android:id="@+id/buttonclose1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@id/textViewSet1"
        android:text="Button" />

</RelativeLayout>

四,表格布局
表格布局就相当于html中的table标签,在这里使用的关键字是TableLayout,此时组件相当于在一个表格中进行摆放,设置TableLayout后就需要设置以下属性
TableRow:行
表格布局的代码:

<TableLayout xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context="com.example.helloandroid.SecondActivity" xmlns:android="http://schemas.android.com/apk/res/android">
    <TableRow>
    <TextView
        android:id="@id/textViewSet1"
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="I am Second" />
    </TableRow>
    <TableRow>
    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_weight="2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="CheckBox" />
    </TableRow>
    <TableRow>
    <Button
        android:id="@+id/buttonclose1"
        android:layout_weight="3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:text="Button" />
     </TableRow>

</TableLayout>

五.框架布局

此布局主要用来存放一个组件,如果有多个组件会按照顺序显示最后一个,其他会被覆盖,但实际依然存在,示例代码如下,关键字为FrameLayout

<FrameLayout xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context="com.example.helloandroid.SecondActivity" xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView
        android:id="@id/textViewSet1"
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="I am Second" />

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_weight="2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="CheckBox" />

    <Button
        android:id="@+id/buttonclose1"
        android:layout_weight="3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:text="Button" />


</FrameLayout>

这就是android主要使用的5种布局,另外这5种布局可以相互嵌套使用,和html的标签是类似的

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章