一、安卓布局的種類
Android共有七大基本布局,
分別是:線性布局LinearLayout、表格布局TableLayout、相對布局RelativeLayout、幀布局FrameLayout、絕對布局AbsoluteLayout、網格布局GridLayout,約束布局ConstraintLayout,
其中,表格布局是線性布局的子類,網格布局是android 4.0后新增的布局,約束布局是Android Studio 2.2推出的新布局,并從Android Studio 2.3開始成為默認布局,
在手機程式設計中,絕對布局基本上不用,用得相對較多的是線性布局和相對布局,以下對線性布局和相對布局大致作一個概述,
二、相對布局
相對布局,顧名思義就是通過相對定位的方式讓控制元件出現在布局的任何位置,
1.常用的基礎控制元件
Button,TextView,EditText,ImageView
2.RelativeLayout中子控制元件常用屬性
1、相對于父控制元件
例如:android:layout_alignParentTop=“true”
android:layout_alignParentTop 控制元件的頂部與父控制元件的頂部對齊;
android:layout_alignParentBottom 控制元件的底部與父控制元件的底部對齊;
android:layout_alignParentLeft 控制元件的左部與父控制元件的左部對齊;
android:layout_alignParentRight 控制元件的右部與父控制元件的右部對齊;
2、相對給定Id控制元件
例如:android:layout_above=“@id/…”
android:layout_above 控制元件的底部置于給定ID的控制元件之上;
android:layout_below 控制元件的底部置于給定ID的控制元件之下;
android:layout_toLeftOf 控制元件的右邊緣與給定ID的控制元件左邊緣對齊;
android:layout_toRightOf 控制元件的左邊緣與給定ID的控制元件右邊緣對齊;
android:layout_alignBaseline 控制元件的baseline與給定ID的baseline對齊;
android:layout_alignTop 控制元件的頂部邊緣與給定ID的頂部邊緣對齊;
android:layout_alignBottom 控制元件的底部邊緣與給定ID的底部邊緣對齊;
android:layout_alignLeft 控制元件的左邊緣與給定ID的左邊緣對齊;
android:layout_alignRight 控制元件的右邊緣與給定ID的右邊緣對齊;
3、居中
例如:android:layout_centerInParent=“true”
android:layout_centerHorizontal 水平居中;
android:layout_centerVertical 垂直居中;
android:layout_centerInParent 父控制元件的中央;
3.示例代碼
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_shopping_menu"
tools:context=".MainActivity" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#4EEE94"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="heavy-sea.智能家居"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:textSize="15dp"
/>
<Button
android:id="@+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="注冊"
android:layout_alignParentRight="true"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="查詢"
android:layout_toLeftOf="@id/b1"
android:layout_marginRight="20dp"
/>
</RelativeLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/pic_rf"
android:layout_centerInParent="true"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/card"
android:layout_centerInParent="true"
android:paddingLeft="100dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/btn_selector"
android:text="刷卡"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:layout_marginBottom="10dp"
/>
</RelativeLayout>
布局顯示:

三、線性布局
線性布局會將它所包含的控制元件在線性方向上依次排列,
在這個布局主要通過設定 android:orientation 屬性來指定布局的排列方式,如果為 vertical 則在垂直方向上線性排列,如果為 horizontal 則會在水平方向上排列,
1.常用屬性
android:orientation 指定布局的排列方式,如果為 vertical 則在垂直方向上線性排列,如果為 horizontal 則會在水平方向上排列,
android:gravity 內部控制元件對齊方式,常用屬性值有center、center_vertical、center_horizontal、top、bottom、left、right等,同時可多個組合,如(left|buttom),
android:layout_gravity 該組件在父容器中的對齊方式
android:id 為該組件設定一個ID,在java檔案中可以通過findViewById(id)找到組件
2.Weight(權重)
android:layout_weight:權重,用來分配當前控制元件在剩余空間的大小,
簡單的用法可概括為要等比例劃分控制元件所在的空間,控制元件本身占空間比例為多少,
3.divider分割線
用于為Linearlayout設定分割線圖片,通過showDividers設定分割線的所在位置
android:divider 設定分割線的圖片
android:showDividers 設定分割線的所在位置,可選none,middle,begining,end
android:dividerPadding 設定分割線的padding
4.示例代碼
line.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line" >
<size
android:height="2dp"
android:width="20dp" />
<stroke android:color="#00ff00" />
</shape>
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_shopping_menu"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="300dp"
android:layout_height="200dp"
android:layout_centerInParent="true"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="200dp"
android:layout_weight="2"
android:divider="@drawable/line"
android:orientation="vertical"
android:showDividers="middle|end" >
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center_horizontal|bottom"
android:text="賬號"
android:textSize="20dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center_horizontal|bottom"
android:text="昵稱"
android:textSize="20dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center_horizontal|bottom"
android:text="密碼"
android:textSize="20dp" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="200dp"
android:layout_weight="4"
android:orientation="vertical" >
<EditText
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<EditText
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<EditText
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
該布局中先整體來一個線性布局,把所有的TextView和所有的EditText分別看成一個整體,二者用orientation指定排列方式為水平的,
再對所有的TextView來一個線性布局,里面所有的TextView用orientation指定排列方式為垂直的,
再對所有的EditText來一個線性布局,里面所有的EditText用orientation指定排列方式為垂直的,
布局顯示

轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/287625.html
標籤:其他
上一篇:C標準輸出流
