今天復(fù)習(xí)Android系統(tǒng)中為我們提供的五大布局:LinearLayout(線性布局)忿偷、RelativeLayout(相對(duì)布局)、FrameLayout(幀布局)绝葡、AbsoluteLayout(絕對(duì)布局)整陌、TablelLayout(表格布局)其中最常用的的是LinearLayout、TablelLayout和RelativeLayout结洼。這些布局都可以嵌套使用。
一详恼、布局介紹
布局 | 介紹/特點(diǎn) | 應(yīng)用場景 |
---|---|---|
LinerLayout(線性布局) | 控件排列方式 = 線性的垂直/水平 | 布局內(nèi)控件按照線性垂直/水平排列 |
RelativeLayout (相對(duì)布局) | 根據(jù)參照物(某個(gè)控件id)來確定控件的位置 | 控件之間存在位置上的聯(lián)系 |
FrameLayout(幀布局) | 放入的控件都被放在左上角且后加入的控件會(huì)重疊覆蓋在之前加入的控件上面 | 控件相互疊加 |
TableLayout(表格布局) | 通過表格形式布局控件位置 | 控件之間存在固定的位置關(guān)系 |
AbsoluteLayout(絕對(duì)布局) | 采用坐標(biāo)的方式定位控件补君,左上角時(shí)(0,0)往右X軸遞增往下Y軸遞增 | 已過時(shí) |
二昧互、布局屬性
1挽铁、公有屬性
即各個(gè)布局都存在的屬性
屬性 | 介紹 | 使用 |
---|---|---|
layout_width 、layout_height | 設(shè)置布局的寬/高 | android:layout_width="wrap_content"http://所需的最小尺寸 android:layout_height="match_parent"http://充滿父布局 android:layout_width="65dp"http://固定寬高 |
layout_margin+方位 | 設(shè)置控件邊緣相對(duì)于父控件的邊距 | layout_margin="10dp"http://設(shè)置四面邊距l(xiāng)ayout_marginTop="10dp"http://設(shè)置上邊距l(xiāng)ayout_marginBottom="10dp"http://設(shè)置下邊距l(xiāng)ayout_marginLeft="10dp"http://設(shè)置左邊距l(xiāng)ayout_marginRight="10dp"http://設(shè)置右邊距 |
padding +方位 | 設(shè)置控件內(nèi)容的邊緣相對(duì)于控件的邊距 | layout_padding="10dp"http://設(shè)置四面邊距l(xiāng)ayout_paddingTop="10dp"http://設(shè)置上邊距l(xiāng)ayout_paddingBottom="10dp"http://設(shè)置下邊距l(xiāng)ayout_paddingLeft="10dp"http://設(shè)置左邊距l(xiāng)ayout_paddingRight="10dp"http://設(shè)置右邊距 |
layout_gravity | 控件相對(duì)父控件的位置 | android:layout_gravity="center"http://居中android:layout_gravity="center_horizontal"http://水平居中android:layout_gravity="center_vertival"http://垂直居中 |
gravity | 控件內(nèi)容相對(duì)控件的位置 | android:layout_gravity="center"http://居中android:layout_gravity="center_horizontal"http://水平居中android:layout_gravity="center_vertival"http://垂直居中 |
2敞掘、特有屬性
布局 | 特有屬性 | 用法 |
---|---|---|
LinerLayout(線性布局) | orientation(設(shè)置控件的排列方式)layout_weight(根據(jù)設(shè)置的權(quán)重叽掘,將布局控件按比例分配,主要設(shè)置在布局內(nèi)的控件中) | android:orientation="horizontal"http://水平android:orientation="vertical"http://垂直 android:layout_weight="1.0"http://設(shè)置權(quán)重 |
RelativeLayout(相對(duì)布局) | layout_alignParentXXX(當(dāng)前控件對(duì)齊父控件的X方位) | android:layout_alignParentTop="true"http://當(dāng)前控件頂端對(duì)齊父控件頂端 android:layout_alignParentBottom="true"http://當(dāng)前控件底端對(duì)齊父控件底端android:layout_alignParentLeft="true"http://當(dāng)前控件左端對(duì)齊父控件左端android:layout_alignParentRight="true"http://當(dāng)前控件右端對(duì)齊父控件右端android:layout_centerInParent="true"http://當(dāng)前控件位于父控件正居中的位置android:layout_centerVertival="true"http://當(dāng)前控件位于父控件垂直居中的位置android:layout_centerHorizontal="true"http://當(dāng)前控件位于父控件水平居中的位置 |
RelativeLayout(相對(duì)布局) | layout_X(當(dāng)前控件位于某個(gè)控件的X方位) | android:layout_above="@id/text"http://當(dāng)前控件位于text控件的上方 android:layout_below="@id/text"http://當(dāng)前控件位于text控件的下方 android:layout_toLeftOf="@id/text"http://當(dāng)前控件位于text控件的左方 android:layout_toRightOf="@id/text"http://當(dāng)前控件位于text控件的右方 android:layout_alignTop="@id/text"http://當(dāng)前控件的頂部 對(duì)齊 text控件的頂部 android:layout_alignRight(End)="@id/text"http://當(dāng)前控件的右部 對(duì)齊 text控件的右部 android:layout_alignBottom="@id/text"http://當(dāng)前控件的底部 對(duì)齊 text控件的底部 android:layout_alignLeft(Start)="@id/text"http://當(dāng)前控件的左部 對(duì)齊 text控件的左部 |
AbsoluteLayout(絕對(duì)布局) | layout_x(指定控件的x坐標(biāo))<p>layout_y(指定控件的Y坐標(biāo)) | android:layout_x="50dp" android:layout_y="50dp" |
TableLayout(表格布局) | TableLayout的行TableRow = 一個(gè)水平排列的線性布局繼承自線性布局故具備線性布局的全部屬性 | |
FrameLayout | 只具備基礎(chǔ)屬性 |
5個(gè)布局元素可相互嵌套使用玖雁,從而實(shí)現(xiàn)各種不同的效果
三更扁、選擇器(selector)
1.作用
通過設(shè)置選擇器(selector)可使控件 在不同操作下(默認(rèn)、點(diǎn)擊等) 顯示不同樣式
通過 xml編寫 = selector.xml
2.屬性
XML屬性 | 說明 | |
---|---|---|
android:drawable | 放一個(gè)drawable資源 | |
android:state_pressed | 按下狀態(tài)赫冬,如一個(gè)按鈕觸摸或者點(diǎn)擊浓镜。 | |
android:state_focused | 取得焦點(diǎn)狀態(tài),比如用戶選擇了一個(gè)文本框劲厌。android:state_hovered | 光標(biāo)懸停狀態(tài)膛薛,通常與focused state相同,它是4.0的新特性 |
android:state_selected | 選中狀態(tài) | |
android:state_enabled | 能夠接受觸摸或者點(diǎn)擊事件 | |
android:state_checked | 被checked了补鼻,如:一個(gè)RadioButton可以被check了哄啄。 | |
android:state_enabled | 能夠接受觸摸或者點(diǎn)擊事件 |
注:上述所有屬性的取值 : boolean屬性 = true、false
3.實(shí)例說明
在drawable添加 selector.xml 資源文件
button_selector.xml:
<?xml version="1.0" encoding="UTF-8"?>
< selector xmlns:android="http://schemas.android.com/apk/res/android">
< !-- 指定按鈕按下時(shí)的圖片 -->
<item android:state_pressed="true"
android:drawable="@drawable/start_down"
/>
< !-- 指定按鈕松開時(shí)的圖片 -->
<item android:state_pressed="false"
android:drawable="@drawable/start"
/>
< /selector>
在布局文件main.xml中控件的屬性設(shè)置:
<Button
android:id="@+id/startButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button_selector"
/>
四风范、布局形狀(Shape)
作用:設(shè)置布局的顏色咨跌、邊框線
使用:通過 xml編寫 = shape.xml
具體使用
<shape xmlns:android="http://schemas.android.com/apk/res/android">
//默認(rèn)顏色
<solid android:color="#876543"/>
//哪個(gè)方向有邊框線
<padding
android:bottom="0dp"
android:left="1dp"
android:right="1dp"
android:top="1dp" />
//邊框線顏色、大小
<stroke
android:width="1dp"
android:color="#000000" />
在布局文件main.xml中控件的屬性設(shè)置:
<Button
android:id="@+id/startButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/layout_shape"/>
寫在最后: 本系列的文章旨在學(xué)習(xí)過程中的總結(jié)硼婿,如果對(duì)你也有幫助锌半,榮幸之至。