零基礎(chǔ)學(xué)鴻蒙編程-UI控件_PositionLayout

什么是PositionLayout

PositionLayout又稱準確位置布局,或絕對定位布局,子控件會指定精確的位置.
這種布局方式一般很少使用,因為屏幕大小千奇百怪,一般都不能指定絕對的位置,否則會很難看。

基礎(chǔ)樣例

效果圖

代碼

<?xml version="1.0" encoding="utf-8"?>
<PositionLayout
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:height="match_parent"
    ohos:width="match_parent">

    <Image
        ohos:height="200vp"
        ohos:width="200vp"
        ohos:image_src="$media:img1"
        ohos:position_x="30vp"
        ohos:position_y="40vp"
        ohos:scale_mode="clip_center"/>

    <Image
        ohos:height="200vp"
        ohos:width="200vp"
        ohos:image_src="$media:img2"
        ohos:position_x="80vp"
        ohos:position_y="260vp"
        ohos:scale_mode="clip_center"/>

    <Image
        ohos:height="200vp"
        ohos:width="200vp"
        ohos:image_src="$media:img3"
        ohos:position_x="120vp"
        ohos:position_y="480vp"
        ohos:scale_mode="clip_center"/>
</PositionLayout>

完整源代碼

https://gitee.com/hspbc/harmonyos_demos/tree/master/positionLayoutDemo

常用屬性說明

屬性名 用途
ohos:width 設(shè)置控件寬度,可設(shè)置為:match_parent(和父控件一樣),match_content(按照內(nèi)容自動伸縮),設(shè)置固定值(如200vp)
ohos:height 設(shè)置控件高度,可設(shè)置為:match_parent(和父控件一樣),match_content(按照內(nèi)容自動伸縮),設(shè)置固定值(如200vp)
ohos:layout_alignment 在父控件內(nèi)對齊方式,可選值:left:居左;start:居左;center:居中;right:居右;end:居右;top:居上;bottom:居下;horizontal_center:水平居中;vertical_center:垂直居中
ohos:background_element 設(shè)置背景,可以是色值(如#FF0000)或圖片等
ohos:visibility 可選值: visible(顯示), invisible(隱藏,但是仍占據(jù)UI空間),hide(隱藏,且不占UI空間)
ohos:position_x 子控件用來設(shè)置橫向坐標位置,樣例:ohos:position_x="30vp"
ohos:position_y 子控件用來設(shè)置縱向坐標位置,樣例:ohos:position_y="30vp"

更多屬性及實際效果,可以在開發(fā)工具里自行體驗.

關(guān)于我

廈門大學(xué)計算機專業(yè) | 前華為工程師
分享編程技術(shù),沒啥深度,但看得懂,適合初學(xué)者。
Java | 安卓 | 前端 | 小程序 | 鴻蒙
公眾號:花生皮編程

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容