日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
HarmonyOS三方件開發(fā)指南(18)-Flexbox流式布局組件

想了解更多內(nèi)容,請(qǐng)?jiān)L問:

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

https://harmonyos.

引言

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

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

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

圖(1)默認(rèn)標(biāo)簽狀態(tài)

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

VideoCache使用指南

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

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

? 修改配置文件

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

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

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

 
 
 
 
  1. //mNames 是item的數(shù)據(jù)源,可以是任意需要顯示的數(shù)據(jù)類型,根據(jù)實(shí)際情況去定義 
  2. parentLayout = (HWFlowViewGroup) findComponentById(ResourceTable.Id_viewgroup); 
  3. parentLayout.HWFlowViewGroup(getContext(), mNames, parentLayout); 
  4. parentLayout.setOnItemClickListener((Component view) -> { 
  5. //item點(diǎn)擊之后的回調(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ā)過程中使用,接下來簡(jiǎn)單的分析下Flexbox 實(shí)現(xiàn)思路

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

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

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

以text為例,計(jì)算每個(gè)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 = DisplayManager.getInstance().getDefaultDisplay(mContext); 
  3.     Point pt = new Point(); 
  4.     display.get().getSize(pt); 
  5.     mParentWidth = (int) pt.getPointX(); 

動(dòng)態(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.     } 

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

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

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

更多原創(chuàng),請(qǐng)關(guān)注:https://harmonyos./column/30

想了解更多內(nèi)容,請(qǐng)?jiān)L問:

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

https://harmonyos.


名稱欄目:HarmonyOS三方件開發(fā)指南(18)-Flexbox流式布局組件
網(wǎng)頁地址:http://www.5511xx.com/article/cogjedc.html