Android布局(RelativeLayout、TableLayout等)使用方法
本文介紹 Android 界面開發(fā)中最基本的四種布局LinearLayout、RelativeLayout、FrameLayout、TableLayout 的使用方法及這四種布局中常用的屬性。
- LinearLayout 線性布局,布局中空間呈線性排列
- RelativeLayout 相對布局,通過相對定位的方式,控制控件位置
- FrameLayout 幀布局,最簡單的布局,所有控件放置左上角
- TableLayout 表格布局,以行列方式控制控件位置
四種布局示例
1.LinearLayout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="150dp"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="垂直1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="垂直2" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="水平1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="水平2" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="150dp"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:text="水平上對齊" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="水平垂直居中" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:text="水平下對齊" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="horizontal">
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:hint="請輸入..."/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="提交" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="horizontal">
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="請輸入..."/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="提交" />
</LinearLayout>
</LinearLayout>
orientation:horizontal(水平)/vertical(垂直),表示線性排列的方向。
layout_width/layout_height:元素的寬度與高度
layout_gravity:top/bottom/center/left/right/etc,表示當(dāng)前元素相對父元素的對齊方式,多種對齊方式用“|”隔開,右上對齊:top|right。
layout_weight:占據(jù)空間的比例,例如元素A和B,A設(shè)置為1,B設(shè)置為3, 元素A、B分別占空間的1/4、3/4,此時(shí)元素寬度不由layout_width決定,設(shè)置為0dp是比較規(guī)范的寫法。
layout_weight 若元素A設(shè)置為1,元素B不設(shè)置,將layout_width設(shè)置為具體的值或wrap_content,那么元素B的寬度由layout_width決定,元素A將占滿屏幕剩下的空間。
2.RelativeLayout
<LinearLayout ...>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="300dp">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:text="我在左下"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="我在中間"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="我在右上"/>
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="300dp">
<Button
android:id="@+id/button_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="參照按鈕"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/button_2"
android:layout_toRightOf="@id/button_2"
android:text="我在右上"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/button_2"
android:layout_toLeftOf="@id/button_2"
android:text="我在左下"/>
</RelativeLayout>
</LinearLayout>
以下屬性值為true/false
layout_centerHorizontal/layout_centerVertical: 水平居中、垂直居中
layout_centerInparent: 相對父元素垂直&水平居中
layout_alignParentBottom: 元素下邊界和父元素下邊界對齊
layout_alignParentLeft: 左邊界對齊
layout_alignParentRight: 右邊界對齊
layout_alignParentTop: 上邊界對齊
以下屬性值為控件id
layout_above/layout_below: 在某元素的上方/下方
layout_toLeftOf/layout_toRightOf: 在某元素的左方/右方
layout_alignTop/layout_alignBottom: 元素上(下)邊界與某元素上(下)邊界對齊
layout_alignLeft/layout_alignRight: 左(右)邊界對齊
3.FrameLayout
所有元素都放置在布局的左上角
4.TableLayout
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow>
<TextView
android:layout_height="wrap_content"
android:text="郵箱"/>
<EditText
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:hint="請輸入您的郵箱" />
</TableRow>
<TableRow>
<TextView
android:layout_height="wrap_content"
android:text="密碼"/>
<EditText
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="請輸入密碼" />
</TableRow>
<TableRow>
<Button
android:layout_height="wrap_content"
android:layout_span="2"
android:text="注冊" />
</TableRow>
</TableLayout>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:stretchColumns="1"> ... </TableLayout>
TableRow: 代表表格布局的一行,行內(nèi)一個(gè)元素代表一列。
layout_span: 合并單元格,設(shè)置為2,代表該元素占據(jù)2列空間。
stretchColumns: TableRow中無法指定空間寬度,那么需要用到該屬性,設(shè)置為1,表示拉伸第2列(0為第1列)與屏幕一樣寬,效果如TableLayout的第二張圖。
5.自定義布局
Android中,布局下可以放置控件,也可以放置子布局。如果子布局內(nèi)容較為獨(dú)立且經(jīng)常使用,例如標(biāo)題欄,或者布局比較復(fù)雜,這時(shí)候可以考慮使用自定義布局的形式導(dǎo)入。方法很簡單。
新建一個(gè)布局文件,例如example.xml
在父布局中引入:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <include layout="@layout/example"/> </LinearLayout>
以上就是Android最基本的四種布局的詳細(xì)內(nèi)容介紹,希望對大家的學(xué)習(xí)有所幫助。
- Android TabLayout(選項(xiàng)卡布局)簡單用法實(shí)例分析
- ExtJS擴(kuò)展 垂直tabLayout實(shí)現(xiàn)代碼
- jQuery EasyUI 中文API Layout(Tabs)
- Android開發(fā)筆記 TableLayout常用的屬性介紹
- Android入門之TableLayout應(yīng)用解析(一)
- Android TableLayout數(shù)據(jù)列表的回顯清空實(shí)現(xiàn)思路及代碼
- Android入門之TableLayout應(yīng)用解析(二)
- Android布局之TableLayout表格布局
- Android開發(fā)之TableLayout表格布局
- android TabLayout使用方法詳解
相關(guān)文章
Android項(xiàng)目開發(fā)常用工具類LightTaskUtils源碼介紹
LightTaskUtils是一個(gè)輕量級(jí)的線程管理工具,本文通過實(shí)例代碼給大家詳細(xì)介紹Android項(xiàng)目開發(fā)常用工具類LightTaskUtils的相關(guān)知識(shí),感興趣的朋友一起看看吧2022-06-06
Android系統(tǒng)聯(lián)系人全特效實(shí)現(xiàn)(上)分組導(dǎo)航和擠壓動(dòng)畫(附源碼)
本文將為大家講解下Android系統(tǒng)聯(lián)系人全特效實(shí)現(xiàn)之分組導(dǎo)航和擠壓動(dòng)畫,具體實(shí)現(xiàn)及源代碼如下,感興趣的朋友可以參考下哈,希望對大家學(xué)習(xí)有所幫助2013-06-06
Android App中用Handler實(shí)現(xiàn)ViewPager頁面的自動(dòng)切換
這篇文章主要介紹了Android App中用Handler實(shí)現(xiàn)ViewPager頁面的自動(dòng)切換的方法,類似于相冊自動(dòng)播放,主要是切換后要提示當(dāng)前頁面所在的位置,需要的朋友可以參考下2016-05-05
Android開發(fā)之超實(shí)用的系統(tǒng)管理工具類【SD卡,網(wǎng)絡(luò),uri,屏幕,網(wǎng)絡(luò),軟鍵盤,文本,進(jìn)程等】
這篇文章主要介紹了Android開發(fā)之超實(shí)用的系統(tǒng)管理工具類,涉及Android針對SD卡,網(wǎng)絡(luò),uri,屏幕,網(wǎng)絡(luò),軟鍵盤,文本,進(jìn)程等相關(guān)操作技巧,需要的朋友可以參考下2018-02-02
Android RecycleView滑動(dòng)停止后自動(dòng)吸附效果的實(shí)現(xiàn)代碼(滑動(dòng)定位)
這篇文章主要介紹了Android RecycleView滑動(dòng)停止后自動(dòng)吸附效果的實(shí)現(xiàn)代碼(滑動(dòng)定位),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10
Android開發(fā)中的MVC設(shè)計(jì)模式淺析
這篇文章主要介紹了Android開發(fā)中的MVC設(shè)計(jì)模式淺析,本文講解了對Android開發(fā)中的MVC設(shè)計(jì)模式的理解,需要的朋友可以參考下2015-06-06

