Android佈局

Android佈局

一.LinearLayout佈局(線性佈局)

有兩種排列方法:

1.從左到右

android:orientation=」horizontal」

代碼以下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" tools:context="com.example.lenovo.androidapplication.MainActivity">

   <TextView  android:layout_width="200dp" android:layout_height="50dp" android:text="Hello!" android:textSize="25dp" android:textColor="#ed450f" android:gravity="center" android:id="@+id/main_name_tv"/>
    <TextView  android:layout_width="200dp" android:layout_height="50dp" android:text="你好!" android:textSize="25dp" android:textColor="#ed450f" android:gravity="center" android:id="@+id/main_pwd_tv"/>

</LinearLayout>

效果如圖

這裏寫圖片描述

2.從上到下

android:orientation=」vertical」

代碼以下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.lenovo.androidapplication.MainActivity">

   <TextView  android:layout_width="match_parent" android:layout_height="50dp" android:text="Hello!" android:textSize="25dp" android:textColor="#ed450f" android:gravity="center" android:id="@+id/main_name_tv"/>
    <TextView  android:layout_width="match_parent" android:layout_height="50dp" android:text="你好!" android:textSize="25dp" android:textColor="#ed450f" android:gravity="center" android:id="@+id/main_pwd_tv"/>

</LinearLayout>

效果如圖

這裏寫圖片描述

二.RelativeLayout佈局(相對佈局)

有三種類型的屬性:

1.屬性值是true或false,例如:

android:layout_centerHrizontal 水平居中

android:layout_centerVertical 垂直居中

android:layout_centerInparent 相對於父元素徹底居中。

android:layout_alignParentBottom 位於父元素的下邊緣

android:layout_alignParentTop 位於父元素的上邊緣

android:layout_alignParentLeft 位於父元素的左邊緣

android:layout_alignParentRight 位於父元素的右邊緣

代碼以下

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.lenovo.androidapplication.MainActivity">

   <TextView  android:layout_width="match_parent" android:layout_height="50dp" android:text="Hello!" android:textSize="25dp" android:textColor="#ed450f" android:gravity="center" android:layout_centerVertical="true" android:id="@+id/main_name_tv"/>


</RelativeLayout>

效果如圖

這裏寫圖片描述

2.屬性值是「@id/*」,例如:

android:layout_below 在某元素的下方

android:layout_above 在某元素的上方

andorid:layout_toRightOf 在某元素的右方

android:layout_toLeftOf 在某元素的左方

android:layout_alignBottom 和某元素下方對齊

android:layout_alignTop 和某元素上方對齊

android:layout_alignRight 和某元素右方對齊

android:layout_alignLeft 和某元素左方對齊

代碼以下

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.lenovo.androidapplication.MainActivity">

   <TextView  android:layout_width="match_parent" android:layout_height="50dp" android:text="Hello!" android:textSize="25dp" android:textColor="#ed450f" android:gravity="center" android:id="@+id/main_name_tv"/>
    <TextView  android:layout_below="@id/main_name_tv" android:layout_width="match_parent" android:layout_height="50dp" android:text="你好!" android:textSize="25dp" android:textColor="#ed450f" android:gravity="center" android:id="@+id/main_pwd_tv"/>

</RelativeLayout>

效果如圖

這裏寫圖片描述

3.屬性值是數值 例如:

android:layout_marginLeft 離某元素左邊緣的距離

android:layout_marginRight 離某元素右邊緣的距離

android:layout_marginTop 離某元素上邊緣的距離

android:layout_marginBottom 離某元素下邊緣的距離

代碼以下

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.lenovo.androidapplication.MainActivity">

   <TextView  android:layout_width="100dp" android:layout_height="50dp" android:text="Hello!" android:textSize="25dp" android:textColor="#ed450f" android:gravity="center" android:id="@+id/main_name_tv"/>
    <TextView  android:layout_marginLeft="200dp" android:layout_width="100dp" android:layout_height="50dp" android:text="你好!" android:textSize="25dp" android:textColor="#ed450f" android:gravity="center" android:id="@+id/main_pwd_tv"/>

</RelativeLayout>

效果如圖

這裏寫圖片描述

三.MyLayout佈局(自定義ViewGroup)

自定義佈局主要是重寫兩個方法:

1.onMeasure() 這個是寫自定義容器的大小.

2.nLayout() 這個是寫子元素的佈局。

四.FrameLayout佈局(幀佈局)

這個佈局的特色是從左上角開始,後面的會覆蓋前面的控件。

五.TableLayout佈局(表格佈局)

它遵循着如下結構:

<TableLayout>
    <TableRow>
    <!-在這裏填充第一行的元素->
    </TableRow>
    <TableRow>
    <!-在這裏填充第二行的元素->
    </TableRow>    
</TableLayout>

還有兩個重要屬性:

1.寫在TableLayout中的屬性

android:stretchColumns 設置第幾列爲伸展(0表示第一列)

android:shrinkColumns 設置第幾列爲收縮

android:collapseColumns 設置第幾列爲隱藏

2.寫在TableRow裏的控件裏的屬性

android:layout_column 設置控件在第幾列

android:layout_span 設置控件能跨多少列

六.AbsoluteLayout佈局(絕對佈局)

定義兩個控件左上角座標軸 ,即android:layout_x 和android:layout_y來控制位置。