RelativeLayout 簡(jiǎn)述
RelativeLayout 繼承于 android.widget.ViewGroup,按照子元素之間的位置關(guān)系完成布局,作為 Android 系統(tǒng)五大布局中最靈活也是最常用的一種布局方式,非常適合于一些比較復(fù)雜的界面設(shè)計(jì)。
RelativeLayout 和 LinearLayout 類似,都是 ViewGroup,能“容納”多個(gè)子view。
RelativeLayout 是一個(gè)以相對(duì)位置顯示子視圖的視圖組。每個(gè)視圖的位置可以指定為相對(duì)于同級(jí)元素的位置(例如,在另一個(gè)視圖的左側(cè)或下方)或相對(duì)于父級(jí) RelativeLayout 區(qū)域的位置(例如在底部、左側(cè)或中心對(duì)齊)。
子 view 可以是 TextView,Button,或者是 LinearLayout,RelativeLayout 等等。 如果不添加其他配置,它們默認(rèn)是在 RelativeLayout 的左上角。
在 RelativeLayout 中,子 View 可以根據(jù)另一個(gè)子 View 來(lái)確定位置。 但必須注意的是,RelativeLayout 和它的子 View 不能互相依賴。比如 RelativeLayout 設(shè)置高度為 wrap_content,子 View 設(shè)置了 ALIGN_PARENT_BOTTOM,這樣你會(huì)發(fā)現(xiàn) RelativeLayout 被撐到最大。 RelativeLayout 能消除嵌套視圖組并使布局層次結(jié)構(gòu)保持扁平化。
屬性介紹
RelativeLayout 屬性:
RelativeLayout 可以指定子視圖相對(duì)于父視圖或彼此(由 ID 確定)的位置。因此,可以按照右邊框?qū)R兩個(gè)元素,或者使它們一上一下,屏幕居中,左側(cè)居中,等等。默認(rèn)情況下,所有子視圖均繪制在布局的左上角,因此必須使用 RelativeLayout.LayoutParams 中提供的各種布局屬性定義每個(gè)視圖的位置。
有很多布局屬性可用于 RelativeLayout 中的視圖,部分示例包括:
android:layout_alignParentTop
如果為 “true”,會(huì)將此視圖的上邊緣與父視圖的上邊緣對(duì)齊。
android:layout_centerVertical
如果為 “true”,會(huì)將此子級(jí)在父級(jí)內(nèi)垂直居中。
android:layout_below
將此視圖的上邊緣放置在使用資源 ID 指定的視圖下方。
android:layout_toRightOf
將此視圖的左邊緣放置在使用資源 ID 指定的視圖右側(cè)。
示例:
為了讓UI好看一點(diǎn),先定義一下樣式,在style.xml文件中新增一個(gè)style。
<style name="RelativeLayoutDemo1Item">
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:padding">4dp</item>
<item name="android:background">@color/colorAccent</item>
<item name="android:textColor">#ffffff</item>
<item name="android:textSize">12sp</item>
</style>
示例1:
在layout中增加RelativeLayout與一些子View。 子View設(shè)置了不同的屬性,分布在父View的上下左右中各個(gè)地方。
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="100dp">
<TextView
style="@style/RelativeLayoutDemo1Item"
android:text="default" />
<TextView
style="@style/RelativeLayoutDemo1Item"
android:layout_alignParentEnd="true"
android:text="layout_alignParentEnd" />
<TextView
style="@style/RelativeLayoutDemo1Item"
android:layout_centerInParent="true"
android:text="layout_centerInParent" />
<TextView
style="@style/RelativeLayoutDemo1Item"
android:layout_alignParentBottom="true"
android:text="layout_alignParentBottom" />
<TextView
style="@style/RelativeLayoutDemo1Item"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:text="layout_alignParentBottom | End" />
</RelativeLayout>
示例2:
子View可以把另外的子View當(dāng)做位置依據(jù)。
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="120dp">
<TextView
android:id="@+id/tv1"
style="@style/RelativeLayoutDemo1Item"
android:text="天" />
<TextView
android:id="@+id/tv2"
style="@style/RelativeLayoutDemo1Item"
android:layout_below="@id/tv1"
android:layout_toEndOf="@id/tv1"
android:text="天" />
<TextView
android:id="@+id/tv3"
style="@style/RelativeLayoutDemo1Item"
android:layout_below="@id/tv2"
android:layout_toEndOf="@id/tv2"
android:text="向" />
<TextView
android:id="@+id/tv4"
style="@style/RelativeLayoutDemo1Item"
android:layout_below="@id/tv3"
android:layout_toEndOf="@id/tv3"
android:text="上" />
</RelativeLayout>
最后分享一份
【騰訊技術(shù)團(tuán)隊(duì)出品】Android零基礎(chǔ)入門到精通,Android Studio安裝教程+全套安卓基礎(chǔ)教程
Android編程入門教程
Java語(yǔ)言基礎(chǔ)從入門到熟悉
Kotlin語(yǔ)言基礎(chǔ)從入門到熟悉
Android 技術(shù)棧從入門到熟悉
Android Jetpack 全家桶全面學(xué)習(xí)
對(duì)于新手來(lái)說(shuō)可能安裝Android Studio存在一定困難你可以看著以下視頻,一步步的跟著學(xué)習(xí)安裝運(yùn)行
Android Studio 安裝教程
有了Java階段的學(xué)習(xí),這一階段建議以視頻學(xué)習(xí)為主輔以圖書查漏補(bǔ)缺。如果以圖書為主,可以根據(jù)圖書講解敲代碼,輔以教學(xué)視頻查漏補(bǔ)缺。遇到問(wèn)題可以去百度,入門的問(wèn)題一般會(huì)有很多人遇到,并且給出比較好的解答。
需要掌握基本知識(shí)點(diǎn),比如四大組件如何使用、如何創(chuàng)建Service、如何進(jìn)行布局、簡(jiǎn)單的自定義View、動(dòng)畫、網(wǎng)絡(luò)通信等常見技術(shù)。
全套零基礎(chǔ)教程已經(jīng)為你們準(zhǔn)備好了,需要的可以添加下方二維碼免費(fèi)領(lǐng)取

全套安卓基礎(chǔ)教程
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-857282.html
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-857282.html
到了這里,關(guān)于Android入門教程 | UI布局之RelativeLayout 相對(duì)布局的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!