鴻蒙單一方向布局實(shí)現(xiàn)音樂(lè)播放UI
想了解更多內(nèi)容,請(qǐng)?jiān)L問(wèn):
51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)
https://harmonyos.51cto.com/#zz
本小節(jié)我們將使用DirectionalLayout(單一方向排列布局,我們也可以將其稱為線性布局)來(lái)實(shí)現(xiàn)下面UI圖的示例。
UI圖拆解
一般我們從UI工程師手里拿到UI界面設(shè)計(jì)圖后,上面有很多尺寸標(biāo)記等屬性。在我們學(xué)習(xí)了所有布局和組件后,我們完全可以使用一個(gè)或者多個(gè)布局和組件組合在一起,實(shí)現(xiàn)復(fù)雜的界面效果。
上面我自己手動(dòng)拖拽了一個(gè)音樂(lè)播放界面,沒(méi)有標(biāo)注各個(gè)屬性值,僅用于學(xué)習(xí)DirectionalLayout布局的使用,不要在意它的美觀。
首先我們拿到后,根據(jù)上面的標(biāo)注信息以及組件功能要點(diǎn)等一系列參數(shù),將整個(gè)UI界面圖根據(jù)布局劃分多個(gè)模塊進(jìn)行父布局占位,然后再根據(jù)劃分的布局來(lái)編寫具體的子組件信息。
我根據(jù)用戶的交互性將整個(gè)頁(yè)面以上下結(jié)構(gòu)劃分為兩部分,上部分為展示型、下部分為控件型。在底部控件區(qū)域,是一系列操作按鈕,它們?cè)谝粋€(gè)布局中,居中排列。
在上部分的展示區(qū)域,我又根據(jù)控件類型將區(qū)域以左右結(jié)構(gòu)劃分為兩個(gè)部分,左區(qū)域顯示歌曲作者頭像,右側(cè)區(qū)域顯示歌曲信息。
在右側(cè)歌曲信息區(qū)域又劃分為上下兩個(gè)區(qū)域,上區(qū)域用于展示歌曲名稱及作詞作曲主唱信息,下區(qū)域顯示歌詞內(nèi)容。
布局占位
在上面我們已經(jīng)通過(guò)不同的功能,不同的特性將整個(gè)UI圖拆解成多個(gè)部分,現(xiàn)在我們將使用DirectionalLayout布局來(lái)進(jìn)行具體布局占位。
① 首先,我們將整個(gè)布局根據(jù)權(quán)重分為上下兩個(gè)區(qū)域,上區(qū)域占4份,下區(qū)域占1份。
- <?xml version="1.0" encoding="utf-8"?>
- <DirectionalLayout xmlns:ohos="http://schemas.huawei.com/res/ohos"
- ohos:width="match_parent"
- ohos:height="match_parent"
- ohos:background_element="#5c6d71"
- ohos:orientation="vertical">
- <DirectionalLayout
- ohos:id="$+id:topLayout"
- ohos:width="match_parent"
- ohos:height="0vp"
- ohos:weight="4">
- <Text
- ohos:width="match_parent"
- ohos:height="match_parent"
- ohos:text="上"/>
- </DirectionalLayout>
- <DirectionalLayout
- ohos:id="$+id:centerLayout"
- ohos:width="match_parent"
- ohos:height="0vp"
- ohos:background_element="#009688"
- ohos:weight="1">
- <Text
- ohos:width="match_parent"
- ohos:height="match_parent"
- ohos:text="下"/>
- </DirectionalLayout>
- </DirectionalLayout>
② 接下來(lái)我們來(lái)進(jìn)行上區(qū)域的左右占位。
- <?xml version="1.0" encoding="utf-8"?>
- <DirectionalLayout xmlns:ohos="http://schemas.huawei.com/res/ohos"
- ohos:width="match_parent"
- ohos:height="match_parent"
- ohos:background_element="#5c6d71"
- ohos:orientation="vertical">
- <DirectionalLayout
- ohos:id="$+id:topLayout"
- ohos:width="match_parent"
- ohos:height="0vp"
- ohos:weight="4"
- ohos:orientation="horizontal">
- <DirectionalLayout
- ohos:id="$+id:leftLayout"
- ohos:width="0vp"
- ohos:height="match_parent"
- ohos:weight="1">
- <Text
- ohos:width="match_parent"
- ohos:height="match_parent"
- ohos:background_element="#0097A7"
- ohos:text="左"/>
- </DirectionalLayout>
- <DirectionalLayout
- ohos:id="$+id:rightLayout"
- ohos:width="0vp"
- ohos:height="match_parent"
- ohos:weight="1">
- <Text
- ohos:width="match_parent"
- ohos:height="match_parent"
- ohos:background_element="#03A9F4"
- ohos:text="右"/>
- </DirectionalLayout>
- </DirectionalLayout>
- <DirectionalLayout
- ohos:id="$+id:centerLayout"
- ohos:width="match_parent"
- ohos:height="0vp"
- ohos:background_element="#009688"
- ohos:weight="1">
- <Text
- ohos:width="match_parent"
- ohos:height="match_parent"
- ohos:text="下"/>
- </DirectionalLayout>
- </DirectionalLayout>
③ 上區(qū)域的左右布局占位我們已經(jīng)完成,接下來(lái)就是上區(qū)域的右側(cè)歌詞區(qū)域占位,是上下區(qū)域占位。
- <?xml version="1.0" encoding="utf-8"?>
- <DirectionalLayout xmlns:ohos="http://schemas.huawei.com/res/ohos"
- ohos:width="match_parent"
- ohos:height="match_parent"
- ohos:background_element="#5c6d71"
- ohos:orientation="vertical">
- <DirectionalLayout
- ohos:id="$+id:topLayout"
- ohos:width="match_parent"
- ohos:height="0vp"
- ohos:weight="4"
- ohos:orientation="horizontal">
- <DirectionalLayout
- ohos:id="$+id:leftLayout"
- ohos:width="0vp"
- ohos:height="match_parent"
- ohos:weight="1">
- <Text
- ohos:width="match_parent"
- ohos:height="match_parent"
- ohos:background_element="#0097A7"
- ohos:text="左"/>
- </DirectionalLayout>
- <DirectionalLayout
- ohos:id="$+id:rightLayout"
- ohos:width="0vp"
- ohos:height="match_parent"
- ohos:weight="1"
- ohos:orientation="vertical">
- <DirectionalLayout
- ohos:id="$+id:rightTopLayout"
- ohos:width="match_parent"
- ohos:height="0vp"
- ohos:weight="1">
- <Text
- ohos:width="match_parent"
- ohos:height="match_parent"
- ohos:background_element="#00796B"
- ohos:text="上"/>
- </DirectionalLayout>
- <DirectionalLayout
- ohos:id="$+id:rightBottomLayout"
- ohos:width="match_parent"
- ohos:height="0vp"
- ohos:weight="4">
- <Text
- ohos:width="match_parent"
- ohos:height="match_parent"
- ohos:background_element="#00BCD4"
- ohos:text="下"/>
- </DirectionalLayout>
- </DirectionalLayout>
- </DirectionalLayout>
- <DirectionalLayout
- ohos:id="$+id:centerLayout"
- ohos:width="match_parent"
- ohos:height="0vp"
- ohos:background_element="#009688"
- ohos:weight="1">
- <Text
- ohos:width="match_parent"
- ohos:height="match_parent"
- ohos:text="下"/>
- </DirectionalLayout>
- </DirectionalLayout>
以上便是我們將拆解的UI圖進(jìn)行代碼布局占位,接下來(lái)我們將根據(jù)各個(gè)區(qū)域的實(shí)際顯示的控件進(jìn)行完善界面。
定義組件實(shí)現(xiàn)UI圖
我們先從上下左右的順序開(kāi)始編寫組件,我們可以看到原圖中左側(cè)是一個(gè)圓形的圖片,用于展示歌曲作者頭像。用于顯示圖像的組件是Image,會(huì)在后續(xù)的章節(jié)中詳細(xì)講解。之前我們是使用DirectionalLayout和Text進(jìn)行占位時(shí),為了明顯期間我們代碼寫的比較啰嗦,在實(shí)際的業(yè)務(wù)中,我們盡可能使用最優(yōu)寫法。
- <DirectionalLayout
- ohos:id="$+id:topLayout"
- ohos:width="match_parent"
- ohos:height="0vp"
- ohos:weight="4"
- ohos:orientation="horizontal">
- <Image
- ohos:id="$+id:leftLayout"
- ohos:width="0vp"
- ohos:height="match_parent"
- ohos:weight="1"
- ohos:image_src="$media:changpian"></Image>
- <DirectionalLayout
- ohos:id="$+id:rightLayout"
- ohos:width="0vp"
- ohos:height="match_parent"
- ohos:weight="1"
- ohos:orientation="vertical">
- <DirectionalLayout
- ohos:id="$+id:rightTopLayout"
- ohos:width="match_parent"
- ohos:height="0vp"
- ohos:weight="1">
- <Text
- ohos:width="match_parent"
- ohos:height="match_parent"
- ohos:background_element="#00796B"
- ohos:text="上"/>
- </DirectionalLayout>
- <DirectionalLayout
- ohos:id="$+id:rightBottomLayout"
- ohos:width="match_parent"
- ohos:height="0vp"
- ohos:weight="4">
- <Text
- ohos:width="match_parent"
- ohos:height="match_parent"
- ohos:background_element="#00BCD4"
- ohos:text="下"/>
- </DirectionalLayout>
- </DirectionalLayout>
- </DirectionalLayout>
討論
為什么高度已經(jīng)設(shè)置成match_parent,圖片還是顯示一半呢?是必須為114px才行嗎?歡迎討論!!!
接下來(lái)我們將實(shí)現(xiàn)右側(cè)的歌詞信息布局中的組件。

上布局區(qū)域我們把組件都給填充好了,接下來(lái)我們將填充下布局區(qū)域的組件。在上邊文字顯示我們目前是以靜態(tài)方式給定的,所以創(chuàng)建了多個(gè)Text組件。
- <DirectionalLayout
- ohos:id="$+id:centerLayout"
- ohos:width="match_parent"
- ohos:height="0vp"
- ohos:weight="1"
- ohos:alignment="center"
- ohos:left_margin="200vp"
- ohos:right_margin="200vp"
- ohos:orientation="horizontal">
- <Image
- ohos:width="match_content"
- ohos:height="match_content"
- ohos:weight="1"
- ohos:image_src="$media:pinghengqi"/>
- <Image
- ohos:width="match_content"
- ohos:height="match_content"
- ohos:weight="1"
- ohos:image_src="$media:kuaitui"/>
- <Image
- ohos:width="match_content"
- ohos:height="match_content"
- ohos:weight="1"
- ohos:image_src="$media:bofang"/>
- <Image
- ohos:width="match_content"
- ohos:height="match_content"
- ohos:weight="1"
- ohos:image_src="$media:kuaijin"/>
- <Image
- ohos:width="match_content"
- ohos:height="match_content"
- ohos:weight="1"
- ohos:image_src="$media:shengyin"/>
- <Image
- ohos:width="match_content"
- ohos:height="match_content"
- ohos:weight="1"
- ohos:image_src="$media:liebiao"/>
- </DirectionalLayout>
我們啟動(dòng)TV模擬器,運(yùn)行查看是否和我們剛開(kāi)始的UI圖相似(一些尺寸上的差異暫時(shí)忽略不計(jì))。

為何圖片顯示一半?
我們?cè)趍edia中存入的圖片應(yīng)該在32-bit color以下,才能全部渲染。
想了解更多內(nèi)容,請(qǐng)?jiān)L問(wèn):
51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)
https://harmonyos.51cto.com/#zz