自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

HarmonyOS三方件開發(fā)指南(18)-Flexbox流式布局組件

開發(fā) OpenHarmony
文章由鴻蒙社區(qū)產(chǎn)出,想要了解更多內(nèi)容請前往:51CTO和華為官方戰(zhàn)略合作共建的鴻蒙技術社區(qū)https://harmonyos.51cto.com

[[394124]]

想了解更多內(nèi)容,請訪問:

51CTO和華為官方合作共建的鴻蒙技術社區(qū)

https://harmonyos.51cto.com

引言

上一篇給大家介紹底部導航欄的組件使用及開發(fā)指南,本篇將給大家?guī)砹硪粋€鴻蒙三方件的是實現(xiàn):Flexbox,何為Flexbox,如果對Java的Swing比較熟悉的話一定不會陌生,就是控件根據(jù)ViewGroup的寬,自動的往右添加,如果當前行剩余空間不足,則自動添加到下一行。有點所有的控件都往左飄的感覺,第一行滿了,往第二行飄~所以也叫流式布局。鴻蒙并沒有提供流式布局,但是某些場合中,流式布局還是非常適合使用的,比如關鍵字標簽,搜索熱詞列表等,比如下圖:

這些都特別適合使用Flexbox,本篇會帶領大家自己實現(xiàn)Flexbox,然后使用我們自己定義的Flexbox實現(xiàn)上面的標簽效果。學會使用一個控件和學會寫一個控件,我相信大家都明白,授人以魚不如授人以漁。

接下來看下鴻蒙模擬器的實現(xiàn)效果,效果圖如下:

圖(1)默認標簽狀態(tài)

圖(2)標簽選中狀態(tài)

VideoCache使用指南

Ø 新建工程, 添加組件Har包依賴

在應用模塊中添加HAR,只需要將flexboxlibrary-debug.har復制到entry\libs目錄下即可

Ø 修改配置文件

1. 在布局中添加如下代碼:

  1. <com.istone.flexboxlibrary.HWFlowViewGroup 
  2.     ohos:id="$+id:viewgroup" 
  3.     ohos:height="match_content" 
  4.     ohos:width="match_parent" 
  5.     ohos:background_element="gray" 
  6.     ohos:orientation="vertical" 
  7.     /> 

2.在代碼中通過以下方式使用:

  1. //mNames 是item的數(shù)據(jù)源,可以是任意需要顯示的數(shù)據(jù)類型,根據(jù)實際情況去定義 
  2. parentLayout = (HWFlowViewGroup) findComponentById(ResourceTable.Id_viewgroup); 
  3. parentLayout.HWFlowViewGroup(getContext(), mNames, parentLayout); 
  4. parentLayout.setOnItemClickListener((Component view) -> { 
  5. //item點擊之后的回調(diào) 
  6.     Text text = (Text)view
  7.     if(text.isSelected()){ 
  8.         text.setTextColor(Color.BLACK); 
  9.  
  10.     }else
  11.         text.setTextColor(Color.WHITE); 
  12.     } 
  13. }); 
  14. 1. 

VideoCache開發(fā)指南

在上述中,已經(jīng)說明Flexbox 如何在開發(fā)過程中使用,接下來簡單的分析下Flexbox 實現(xiàn)思路

1、對于Flexbox ,需要指定的LayoutConfig,我們目前只需要能夠識別margin、padding即可

2、measureChild中計算所有childView的寬度,然后根據(jù)childView的寬度,計算當前每一行的寬度

3、最后根據(jù)計算之后的寬度,對中所有的childView進行布局。

以text為例,計算每個childView 的代碼如下:

  1. private float measureChild(Text text) { 
  2.     Paint paint = new Paint(); 
  3.     paint.setTextSize(text.getTextSize()); 
  4.     float childWidth = paint.measureText(text.getText()); 
  5.     childWidth = childWidth + text.getPaddingLeft() + text.getPaddingRight() + text.getMarginLeft() + text.getMarginRight(); 
  6.     return childWidth; 
  7. 1. 

初始化每行的布局,代碼如下:

  1. private DirectionalLayout initDirtLayout() { 
  2.     DirectionalLayout childLayout = new DirectionalLayout(mContext); 
  3.     childLayout.setOrientation(Component.HORIZONTAL); 
  4.     DirectionalLayout.LayoutConfig layoutConfig = new DirectionalLayout.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT, ComponentContainer.LayoutConfig.MATCH_CONTENT); 
  5.     layoutConfig.setMargins(10, 10, 10, 10); 
  6.     childLayout.setLayoutConfig(layoutConfig); 
  7.     return childLayout; 

獲取屏幕的寬度,代碼如下:

  1. private void getParentWidthAndHeight() { 
  2.     Optional<Display> display = DisplayManager.getInstance().getDefaultDisplay(mContext); 
  3.     Point pt = new Point(); 
  4.     display.get().getSize(pt); 
  5.     mParentWidth = (int) pt.getPointX(); 

動態(tài)布局:

  1. private void initChildViews() { 
  2.     for (int i = 0; i < mNames.length; i++) { 
  3.         Text text = new Text(mContext); 
  4.         text.setId(i); 
  5.         text.setText(mNames[i]); 
  6.         text.setTextSize(100); 
  7.         text.setSelected(false); 
  8.         text.setTextColor(Color.WHITE); 
  9.         text.setPadding(10, 10, 10, 10); 
  10.         ShapeElement background = new ShapeElement(); 
  11.         background.setRgbColor(new RgbColor(205, 92, 92)); 
  12.         text.setBackground(background); 
  13.         DirectionalLayout.LayoutConfig layoutConfig = new DirectionalLayout.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_CONTENT, ComponentContainer.LayoutConfig.MATCH_CONTENT); 
  14.         layoutConfig.setMargins(20, 10, 20, 10); 
  15.         text.setLayoutConfig(layoutConfig); 
  16.  
  17.         if (i == 0) { 
  18.             childLayout = initDirtLayout(); 
  19.             mLineWidth = measureChild(text); 
  20.             childLayout.addComponent(text); 
  21.         } else { 
  22.             mLineWidth = mLineWidth + measureChild(text); 
  23.             if (mLineWidth > (mParentWidth - childLayout.getMarginLeft() - childLayout.getMarginRight())) { 
  24.                 mParentLayout.addComponent(childLayout); 
  25.                 childLayout = initDirtLayout(); 
  26.                 mLineWidth = measureChild(text); 
  27.             } 
  28.             childLayout.addComponent(text); 
  29.             if (i == mNames.length - 1) { 
  30.                 mParentLayout.addComponent(childLayout); 
  31.             } 
  32.         } 
  33.  
  34.         ComponentBean bean = new ComponentBean(text, false); 
  35.         list.add(bean); 
  36.  
  37.         text.setClickedListener(component -> { 
  38.             text.setSelected(!text.isSelected()); 
  39.             mOnItemClickListener.onItemClick(text); 
  40.         }); 
  41.     } 

定義接口,實現(xiàn)item的點擊事件

  1. public interface OnItemClickListener { 
  2.     void onItemClick(Component view); 
  3.  
  4. public void setOnItemClickListener(OnItemClickListener onItemClickListener) { 
  5.     mOnItemClickListener = onItemClickListener; 

按照思路看下來,是不是很簡單呢?我們只需要把握住如何計算childview 的寬度,以及什么情況下添加新的一行即可。

更多原創(chuàng),請關注:https://harmonyos.51cto.com/column/30

想了解更多內(nèi)容,請訪問:

51CTO和華為官方合作共建的鴻蒙技術社區(qū)

https://harmonyos.51cto.com

 

責任編輯:jianghua 來源: 鴻蒙社區(qū)
相關推薦

2021-06-28 14:48:03

鴻蒙HarmonyOS應用

2021-01-18 09:52:20

鴻蒙HarmonyOS開發(fā)

2021-02-04 09:45:19

鴻蒙HarmonyOS應用開發(fā)

2021-01-12 12:04:40

鴻蒙HarmonyOS應用開發(fā)

2021-01-21 13:21:18

鴻蒙HarmonyOSPhotoview組件

2021-01-20 09:54:56

鴻蒙HarmonyOS開發(fā)

2021-03-01 09:48:24

鴻蒙HarmonyOS應用開發(fā)

2021-02-24 15:22:47

鴻蒙HarmonyOS應用開發(fā)

2021-05-12 15:17:39

鴻蒙HarmonyOS應用

2021-01-13 09:40:31

鴻蒙HarmonyOS開發(fā)

2021-02-04 13:06:38

鴻蒙HarmonyOS應用開發(fā)

2021-04-16 09:28:18

鴻蒙HarmonyOS應用

2021-01-22 17:33:03

鴻蒙HarmonyOS應用開發(fā)

2021-03-19 17:42:01

鴻蒙HarmonyOS應用開發(fā)

2021-02-26 14:15:27

鴻蒙HarmonyOS應用開發(fā)

2021-03-01 14:01:41

鴻蒙HarmonyOS應用開發(fā)

2021-04-12 09:36:54

鴻蒙HarmonyOS應用

2021-03-31 09:50:25

鴻蒙HarmonyOS應用開發(fā)

2021-07-28 09:40:04

鴻蒙HarmonyOS應用

2021-03-10 15:03:40

鴻蒙HarmonyOS應用
點贊
收藏

51CTO技術棧公眾號