日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

HarmonyOS之常用布局DirectionalLayout的使用

發(fā)布時間:2024/5/21 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HarmonyOS之常用布局DirectionalLayout的使用 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

一、DirectionalLayout

  • DirectionalLayout 是 Java UI 中的一種重要組件布局,用于將一組組件(Component)按照水平或者垂直方向排布,能夠方便地對齊布局內(nèi)的組件。該布局和其他布局的組合,可以實現(xiàn)更加豐富的布局方式。
  • DirectionalLayout 示意如下:

二、支持的 XML 屬性

  • DirectionalLayout 的共有 XML 屬性繼承自 Component,詳情請參考我之前的博客:HarmonyOS之組件通用的XML屬性總覽。
  • DirectionalLayout 的自有 XML 屬性見下表:
屬性名稱中文描述取值取值說明使用案例alignment對齊方式left表示左對齊可以設(shè)置取值項如表中所列,也可以使用“|”進行多項組合。
ohos:alignment="top|left"
ohos:alignment="left"top表示頂部對齊right表示右對齊bottom表示底部對齊horizontal_center表示水平居中對齊vertical_center表示垂直居中對齊center表示居中對齊start表示靠起始端對齊end表示靠結(jié)束端對齊total_weight所有子視圖的權(quán)重之和float類型可以直接設(shè)置浮點數(shù)值,也可以引用float浮點數(shù)資源ohos:total_weight="2.5"
ohos:total_weight="$float:total_weight"orientation子布局排列方向horizontal表示水平方向布局ohos:orientation="horizontal"vertical表示垂直方向布局ohos:orientation="vertical"
  • DirectionalLayout 所包含組件可支持的 XML 屬性見下表:
屬性名稱中文描述取值取值說明使用案例layout_alignment對齊方式left表示左對齊可以設(shè)置取值項如表中所列,也可以使用“|”進行多項組合。
ohos:layout_alignment="top"
ohos:layout_alignment="top|left"top表示頂部對齊right表示右對齊bottom表示底部對齊horizontal_center表示水平居中對齊vertical_center表示垂直居中對齊center表示居中對齊weight比重float類型可以直接設(shè)置浮點數(shù)值,也可以引用float浮點數(shù)資源ohos:weight="1"
ohos:weight="$float:weight"

三、排列方式

  • DirectionalLayout 的排列方向(orientation)分為水平(horizontal)或者垂直(vertical)方向。
  • 使用 orientation 設(shè)置布局內(nèi)組件的排列方式,默認(rèn)為垂直排列。
① 垂直排列
  • 垂直方向排列三個按鈕,效果如下:

  • 示例代碼如下:
<?xml version="1.0" encoding="utf-8"?><DirectionalLayoutxmlns:ohos="http://schemas.huawei.com/res/ohos"ohos:width="match_parent"ohos:height="match_content"ohos:orientation="vertical"><Buttonohos:width="33vp"ohos:height="20vp"ohos:bottom_margin="3vp"ohos:left_margin="13vp"ohos:background_element="$graphic:color_cyan_element"ohos:text="Button 1"/><Buttonohos:width="33vp"ohos:height="20vp"ohos:bottom_margin="3vp"ohos:left_margin="13vp"ohos:background_element="$graphic:color_cyan_element"ohos:text="Button 2"/><Buttonohos:width="33vp"ohos:height="20vp"ohos:bottom_margin="3vp"ohos:left_margin="13vp"ohos:background_element="$graphic:color_cyan_element"ohos:text="Button 3"/></DirectionalLayout>
  • color_cyan_element.xml:
<?xml version="1.0" encoding="utf-8"?><shape xmlns:ohos="http://schemas.huawei.com/res/ohos"ohos:shape="rectangle"><solidohos:color="#00FFFD"/></shape>
② 水平排列
  • 水平方向排列三個按鈕,效果如下:

  • 示例代碼:
<?xml version="1.0" encoding="utf-8"?><DirectionalLayoutxmlns:ohos="http://schemas.huawei.com/res/ohos"ohos:width="match_parent"ohos:height="match_content"ohos:orientation="horizontal"><Buttonohos:width="33vp"ohos:height="20vp"ohos:left_margin="13vp"ohos:background_element="$graphic:color_cyan_element"ohos:text="Button 1"/><Buttonohos:width="33vp"ohos:height="20vp"ohos:left_margin="13vp"ohos:background_element="$graphic:color_cyan_element"ohos:text="Button 2"/><Buttonohos:width="33vp"ohos:height="20vp"ohos:left_margin="13vp"ohos:background_element="$graphic:color_cyan_element"ohos:text="Button 3"/></DirectionalLayout>
  • color_cyan_element.xml:
<?xml version="1.0" encoding="utf-8"?><shape xmlns:ohos="http://schemas.huawei.com/res/ohos"ohos:shape="rectangle"><solidohos:color="#00FFFD"/></shape>
  • DirectionalLayout 不會自動換行,其子組件會按照設(shè)定的方向依次排列,若超過布局本身的大小,超出布局大小的部分將不會被顯示,例如:
<?xml version="1.0" encoding="utf-8"?><DirectionalLayoutxmlns:ohos="http://schemas.huawei.com/res/ohos"ohos:width="match_parent"ohos:height="20vp"ohos:orientation="horizontal"><Buttonohos:width="166vp"ohos:height="match_content"ohos:left_margin="13vp"ohos:background_element="$graphic:color_cyan_element"ohos:text="Button 1"/><Buttonohos:width="166vp"ohos:height="match_content"ohos:left_margin="13vp"ohos:background_element="$graphic:color_cyan_element"ohos:text="Button 2"/><Buttonohos:width="166vp"ohos:height="match_content"ohos:left_margin="13vp"ohos:background_element="$graphic:color_cyan_element"ohos:text="Button 3"/></DirectionalLayout>
  • color_cyan_element.xml:
<?xml version="1.0" encoding="utf-8"?><shape xmlns:ohos="http://schemas.huawei.com/res/ohos"ohos:shape="rectangle"><solidohos:color="#00FFFD"/></shape>
  • 此布局包含了三個按鈕,但由于 DirectionalLayout 不會自動換行,超出布局大小的組件部分無法顯示。界面顯示如下:

四、對齊方式

  • DirectionalLayout 中的組件使用 layout_alignment 控制自身在布局中的對齊方式,當(dāng)對齊方式與排列方式方向一致時,對齊方式不會生效,如設(shè)置了水平方向的排列方式,則左對齊、右對齊將不會生效。
  • 三種對齊方式的示例代碼:
<?xml version="1.0" encoding="utf-8"?><DirectionalLayoutxmlns:ohos="http://schemas.huawei.com/res/ohos"ohos:width="match_parent"ohos:height="60vp"><Buttonohos:width="50vp"ohos:height="20vp"ohos:background_element="$graphic:color_cyan_element"ohos:layout_alignment="left"ohos:text="Button 1"/><Buttonohos:width="50vp"ohos:height="20vp"ohos:background_element="$graphic:color_cyan_element"ohos:layout_alignment="horizontal_center"ohos:text="Button 2"/><Buttonohos:width="50vp"ohos:height="20vp"ohos:background_element="$graphic:color_cyan_element"ohos:layout_alignment="right"ohos:text="Button 3"/></DirectionalLayout>
  • color_cyan_element.xml:
<?xml version="1.0" encoding="utf-8"?><shape xmlns:ohos="http://schemas.huawei.com/res/ohos"ohos:shape="rectangle"><solidohos:color="#00FFFD"/></shape>
  • 三種對齊方式效果示例:

五、權(quán)重

  • 權(quán)重(weight)就是按比例來分配組件占用父組件的大小,在水平布局下計算公式為:
    父布局可分配寬度=父布局寬度-所有子組件 width 之和;
    組件寬度=組件 weight /所有組件 weight 之和*父布局可分配寬度;
  • 實際使用過程中,建議使用 width=0 來按比例分配父布局的寬度,1:1:1效果如下:

  • 示例代碼:
<?xml version="1.0" encoding="utf-8"?><DirectionalLayoutxmlns:ohos="http://schemas.huawei.com/res/ohos"ohos:width="match_parent"ohos:height="match_content"ohos:orientation="horizontal"><Buttonohos:width="0vp"ohos:height="20vp"ohos:weight="1"ohos:background_element="$graphic:color_cyan_element"ohos:text="Button 1"/><Buttonohos:width="0vp"ohos:height="20vp"ohos:weight="1"ohos:background_element="$graphic:color_gray_element"ohos:text="Button 2"/><Buttonohos:width="0vp"ohos:height="20vp"ohos:weight="1"ohos:background_element="$graphic:color_cyan_element"ohos:text="Button 3"/></DirectionalLayout>
  • color_cyan_element.xml:
<?xml version="1.0" encoding="utf-8"?><shape xmlns:ohos="http://schemas.huawei.com/res/ohos"ohos:shape="rectangle"><solidohos:color="#00FFFD"/></shape>
  • color_gray_element.xml:
<?xml version="1.0" encoding="utf-8"?><shape xmlns:ohos="http://schemas.huawei.com/res/ohos"ohos:shape="rectangle"><solidohos:color="#878787"/></shape>

六、場景示例

  • 源碼示例:
<?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="$graphic:color_light_gray_element"><DirectionalLayoutohos:width="match_parent"ohos:height="match_content"ohos:orientation="vertical"><Buttonohos:width="33vp"ohos:height="20vp"ohos:bottom_margin="3vp"ohos:left_margin="13vp"ohos:background_element="$graphic:color_cyan_element"ohos:text="Button 1"/><Buttonohos:width="33vp"ohos:height="20vp"ohos:bottom_margin="3vp"ohos:left_margin="13vp"ohos:background_element="$graphic:color_cyan_element"ohos:text="Button 2"/><Buttonohos:width="33vp"ohos:height="20vp"ohos:bottom_margin="3vp"ohos:left_margin="13vp"ohos:background_element="$graphic:color_cyan_element"ohos:text="Button 3"/></DirectionalLayout><Component ohos:height="20vp"/><DirectionalLayoutohos:width="match_parent"ohos:height="match_content"ohos:orientation="horizontal"><Buttonohos:width="33vp"ohos:height="20vp"ohos:left_margin="13vp"ohos:background_element="$graphic:color_cyan_element"ohos:text="Button 1"/><Buttonohos:width="33vp"ohos:height="20vp"ohos:left_margin="13vp"ohos:background_element="$graphic:color_cyan_element"ohos:text="Button 2"/><Buttonohos:width="33vp"ohos:height="20vp"ohos:left_margin="13vp"ohos:background_element="$graphic:color_cyan_element"ohos:text="Button 3"/></DirectionalLayout><Component ohos:height="20vp"/><DirectionalLayoutohos:width="match_parent"ohos:height="20vp"ohos:orientation="horizontal"><Buttonohos:width="166vp"ohos:height="match_content"ohos:left_margin="13vp"ohos:background_element="$graphic:color_cyan_element"ohos:text="Button 1"/><Buttonohos:width="166vp"ohos:height="match_content"ohos:left_margin="13vp"ohos:background_element="$graphic:color_cyan_element"ohos:text="Button 2"/><Buttonohos:width="166vp"ohos:height="match_content"ohos:left_margin="13vp"ohos:background_element="$graphic:color_cyan_element"ohos:text="Button 3"/></DirectionalLayout><Component ohos:height="20vp"/><DirectionalLayoutohos:width="match_parent"ohos:height="60vp"><Buttonohos:width="50vp"ohos:height="20vp"ohos:background_element="$graphic:color_cyan_element"ohos:layout_alignment="left"ohos:text="Button 1"/><Buttonohos:width="50vp"ohos:height="20vp"ohos:background_element="$graphic:color_cyan_element"ohos:layout_alignment="horizontal_center"ohos:text="Button 2"/><Buttonohos:width="50vp"ohos:height="20vp"ohos:background_element="$graphic:color_cyan_element"ohos:layout_alignment="right"ohos:text="Button 3"/></DirectionalLayout><Component ohos:height="20vp"/><DirectionalLayoutohos:width="match_parent"ohos:height="match_content"ohos:orientation="horizontal"><Buttonohos:width="0vp"ohos:height="20vp"ohos:weight="1"ohos:background_element="$graphic:color_cyan_element"ohos:text="Button 1"/><Buttonohos:width="0vp"ohos:height="20vp"ohos:weight="1"ohos:background_element="$graphic:color_gray_element"ohos:text="Button 2"/><Buttonohos:width="0vp"ohos:height="20vp"ohos:weight="1"ohos:background_element="$graphic:color_cyan_element"ohos:text="Button 3"/></DirectionalLayout> </DirectionalLayout>

總結(jié)

以上是生活随笔為你收集整理的HarmonyOS之常用布局DirectionalLayout的使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 韩国伦理电影免费在线 | 国产男男gay网站 | 欧美毛片在线 | 国产嫩草av | 91尤物国产福利在线观看 | 欧美成人三区 | 国产精品亚洲天堂 | 老熟妇毛片 | 亚洲熟伦熟女新五十路熟妇 | 日本不卡中文字幕 | 一本色道久久综合亚洲精品 | 夜夜草视频| 国产剧情一区在线 | 欧美做爰猛烈床戏大尺度 | 99热国产| 韩国久久久久久 | 粗喘呻吟撞击猛烈疯狂 | 香蕉视频传媒 | 大香伊人中文字幕精品 | 色精品视频| 一级片免费在线观看 | 国产精品入口免费 | 国产一区二区三区麻豆 | 国产精品资源在线观看 | 亚洲成人久 | 91大奶| 亚洲少妇自拍 | 妺妺窝人体色www婷婷 | 国产三级在线播放 | 屁屁影院第一页 | 中文在线观看免费高清 | 免费一级a毛片 | 国产精品自偷自拍 | 午夜激情小视频 | 精品国产99久久久久久宅男i | 久久中文字幕国产 | 另类综合在线 | 国产ts三人妖大战直男 | 日韩一区二区三区四区五区六区 | 亚洲一区二区三区在线看 | 天堂网视频在线 | 男女做激情爱呻吟口述全过程 | 深夜福利av | 美女黄色小视频 | 中文av在线播放 | 久久亚洲精少妇毛片午夜无码 | 91中文字幕在线播放 | 生活片一级片 | 18无套直看片红桃 | 日本色站 | 日本xxxxxwwwww| 欧美xo影院 | www超碰 | 91成人在线观看喷潮动漫 | 欧美日韩在线一区二区 | 麻豆天天躁天天揉揉av | 欧美福利在线观看 | 雨宫琴音一区二区三区 | 国产91丝袜在线播放九色 | 香蕉视频2020 | 成人做爰免费视频免费看 | 九九九九久久久久 | 亚洲国产网站 | 国产天堂视频 | 成人免费大片黄在线播放 | 中文字幕一区二区三区免费看 | 日韩高清黄色 | 一级中文字幕 | 日本a级大片 | 99久久精品免费看国产 | 国产黄a三级三级三级看三级男男 | 国产白浆视频 | 午夜久久一区 | 超碰1000| 亚洲国产成人一区 | 成人网色 | 丰满大乳国产精品 | 欧美顶级metart裸体全部自慰 | 天天干夜操 | 嫩嫩av| 杨幂毛片 | 奇米成人 | 熟妇人妻系列aⅴ无码专区友真希 | 中文字幕一二三四区 | 欧美日韩丝袜 | 波多野结衣免费视频观看 | 日韩免费av一区 | 手机看片福利一区 | 黑人与日本少妇高潮 | 国产福利视频一区二区 | 久久久久久久久久久国产精品 | 一级淫片免费看 | 日本欧美国产一区二区三区 | 黄色一级在线视频 | 40到50岁中老年妇女毛片 | www激情 | 亚洲综合精品国产一区二区三区 | 亚洲一区二区免费 | 亚洲码欧美码一区二区三区 |