鸿蒙HarmonyOS三方件开发指南(17)-BottomNavigationBar

开发 OpenHarmony
文章由鸿蒙社区产出,想要了解更多内容请前往:51CTO和华为官方战略合作共建的鸿蒙技术社区https://harmonyos.51cto.com

[[393383]]

想了解更多内容,请访问:

51CTO和华为官方合作共建的鸿蒙技术社区

https://harmonyos.51cto.com

引言

BottomNavigationBar底部导航栏,可以说所有的app是这样的页面架构,原因很简单,操作简单,模块化清晰,页面切换流畅,而且每页都可以展示不同的风格。相信开发者已经很熟悉Android的底部导航栏的开发以及开发流程,那么接下来将对比Android来讲解鸿蒙的底部导航栏的实现步骤。

功能介绍

鸿蒙BottomNavigationBar底部导航栏,根据所需要底部button的数量,动态生成对应的底部button,并且可以设置默认字体颜色,选中字体颜色,默认icon,选中icon属性。模拟器效果图如下:

看了效果图,是不是都想知道在实际工作中,是如何使用的呢?接下来给大家详细介绍下BottomNavigationBar如何使用。

BottomNavigationBar使用指南

Ø 新建工程, 添加组件Har包依赖

在应用模块中添加HAR,只需要将mylibrarybottom-debug.har复制到entry\libs目录下即可。

Ø 修改相关文件

1. 修改主页面的布局文件ability_main.xml:

2. 修改MainAbilitySlice代码:

3. 修改BaseAbilitySlinct代码:

4. MainAbility的代码:

配置好1-4步,接下来就看如何给对应的底部导航栏添加Fraction

1. initBottom 方法如下:

  1. private void initBottom() { 
  2.     tabBottomLayout = (BottomNavigationBar)  mAbilitySliceProvider.findComponentById(ResourceTable.Id_bottom_navigation_bar); 
  3.     bottomInfoList = new ArrayList<>(); 
  4.     // 获取string.json文件中定义的字符串 
  5.     String home = mAbilitySliceProvider.getString(ResourceTable.String_home); 
  6.     String favorite = mAbilitySliceProvider.getString(ResourceTable.String_favorite); 
  7.     String category = mAbilitySliceProvider.getString(ResourceTable.String_category); 
  8.     String profile = mAbilitySliceProvider.getString(ResourceTable.String_mine); 
  9.     // 首页 
  10.     BottomBarInfo<Integer> homeInfo = new BottomBarInfo<>(home, 
  11.             ResourceTable.Media_category_norma1, 
  12.             ResourceTable.Media_category_norma2, 
  13.             defaultColor, tintColor); 
  14.     homeInfo.fraction = HomeFraction.class; 
  15.     // 收藏 
  16.     BottomBarInfo<Integer> favoriteInfo = new BottomBarInfo<>(favorite, 
  17.             ResourceTable.Media_category_norma1, 
  18.             ResourceTable.Media_category_norma2, 
  19.             defaultColor, tintColor); 
  20.     favoriteInfo.fraction = SecondFraction.class; 
  21.     // 分类 
  22.     BottomBarInfo<Integer> categoryInfo = new BottomBarInfo<>(category, 
  23.             ResourceTable.Media_category_norma1, 
  24.             ResourceTable.Media_category_norma2, 
  25.             defaultColor, tintColor); 
  26.     categoryInfo.fraction = ThirdFraction.class; 
  27.     // 我的 
  28.     BottomBarInfo<Integer> profileInfo = new BottomBarInfo<>(profile, 
  29.             ResourceTable.Media_category_norma1, 
  30.             ResourceTable.Media_category_norma2, 
  31.             defaultColor, tintColor); 
  32.     profileInfo.fraction = MineFraction.class; 
  33.  
  34.     // 将每个条目的数据放入到集合 
  35.     bottomInfoList.add(homeInfo); 
  36.     bottomInfoList.add(favoriteInfo); 
  37.     bottomInfoList.add(categoryInfo); 
  38.     bottomInfoList.add(profileInfo); 
  39.     // 设置底部导航栏的透明度 
  40.     tabBottomLayout.setBarBottomAlpha(0.85f); 
  41.     // 初始化所有的条目 
  42.     tabBottomLayout.initInfo(bottomInfoList); 
  43.     initFractionBarComponent(); 
  44.     tabBottomLayout.addBarSelectedChangeListener((index, prevInfo, nextInfo) -> 
  45.             // 显示fraction 
  46.             mFractionBarComponent.setCurrentItem(index)); 
  47.     // 设置默认选中的条目,该方法一定要在最后调用 
  48.     tabBottomLayout.defaultSelected(homeInfo); 

2. 创建fraction类,继承BaseFraction

(1)引入需要展示页面的布局文件

  1. @Override 
  2. blic int getUIComponent() { 
  3.   return ResourceTable.Layout_layout_fraction_home; 

(2)操作布局文件中的控件

  1. @Override 
  2. public void initComponent(Component component) { 
  3.     text = (Text) component.findComponentById(ResourceTable.Id_text); 

BottomNavigationBar开发指南

底部导航栏,在应用中真的非常常见,核心思想就是底部有几个选项,然后点击其中任意一个,切换至对应的页面。接下来主要介绍下核心实现步骤。

主要封装的原则是,动态的,通过外界传递,固定过的则封装起来。其中底部导航栏的图片、文字、文字的颜色是变的,其它的可以封装起来,外界只需要把每个条目的图片、文字以及文字的颜色传入进来即可,内部来实现底部导航栏。在封装的时候,需要面向接口编程,同时使用泛型。

定义接口IBarLayout

1、定义一个IBarLayout接口,第一个泛型就是底部导航栏中的每个条目,第二个泛型是每个条目的数据。在接口里面提供一些方法,可以根据数据查找条目,可以添加监听,可以设置默认选中的条目,可以初始化所有的条目,当某个条目被选中后需要通过回调方法。

代码如下:

  1. public interface IBarLayout<Bar extends ComponentContainer, D> { 
  2.  
  3.     /** 
  4.      * 根据数据查找条目 
  5.      * 
  6.      * @param info 数据 
  7.      * @return 条目 
  8.      */ 
  9.     Bar findBar(D info); 
  10.  
  11.     /** 
  12.      * 添加监听 
  13.      * 
  14.      * @param listener 
  15.      */ 
  16.     void addBarSelectedChangeListener(OnBarSelectedListener<D> listener); 
  17.  
  18.     /** 
  19.      * 默认选中的条目 
  20.      * 
  21.      * @param defaultInfo 
  22.      */ 
  23.     void defaultSelected(D defaultInfo); 
  24.  
  25.     /** 
  26.      * 初始化所有的条目 
  27.      * 
  28.      * @param infoList 
  29.      */ 
  30.     void initInfo(List<D> infoList); 
  31.  
  32.     interface OnBarSelectedListener<D> { 
  33.  
  34.         /** 
  35.          * 当某个条目被选中后的回调,该方法会被调用多次 
  36.          * 
  37.          * @param index 点击后选中条目的下标 
  38.          * @param preInfo 点击前选中的条目 
  39.          * @param nextInfo 点击后选中的条目 
  40.          */ 
  41.         void onBarSelectedChange(int index, D preInfo, D nextInfo); 
  42.     } 

2、再定义一个单个条目的接口IBar,泛型就是每个条目的数据,接口里面定义方法,可以设置条目的数据,可以动态修改某个条目的大小

代码如下:

  1. /** 
  2.  * 单个条目的接口 
  3.  */ 
  4. public interface IBar<D> extends IBarLayout.OnBarSelectedListener<D> { 
  5.  
  6.     /** 
  7.      * 设置条目的数据 
  8.      * 
  9.      * @param data 
  10.      */ 
  11.     void setBarInfo(D data); 
  12.  
  13.     /** 
  14.      * 动态修改某个条目的大小 
  15.      * 
  16.      * @param height 
  17.      */ 
  18.     void resetHeight(int height); 

每个条目所对应的实体类BottomBarInfo

每个条目都有自己的图片、文字、文字的颜色,我们把这些属性定义在一个实体类中。由于颜色可以是整型,也可以是字符串,这里定义泛型,泛型就是文字的颜色。具体是哪种类型的颜色,由调用者来决定。

注意下BarType这个枚举,我们的底部导航栏支持两种类型,IMAGE代表下图,某个条目只显示图片,也可以让某个条目凸出来,只需要将条目的高度变高即可。

  1. public class BottomBarInfo<Color> extends TopBottomBarInfo { 
  2.  
  3.     public enum BarType { 
  4.         /** 
  5.          * 显示图片和文案 
  6.          */ 
  7.         IMAGE_TEXT, 
  8.         /** 
  9.          * 只显示图片 
  10.          */ 
  11.         IMAGE 
  12.     } 
  13.  
  14.     /** 
  15.      * 条目的名称 
  16.      */ 
  17.     public String name
  18.     public BarType tabType; 
  19.     public Class<? extends Fraction> fraction; 
  20.  
  21.     public BottomBarInfo(String nameint defaultImage, int selectedImage) { 
  22.         this.name = name
  23.         this.defaultImage = defaultImage; 
  24.         this.selectedImage = selectedImage; 
  25.         this.tabType = BarType.IMAGE; 
  26.     } 
  27.  
  28.     public BottomBarInfo(String nameint defaultImage, int selectedImage, Color defaultColor, Color tintColor) { 
  29.         this.name = name
  30.         this.defaultImage = defaultImage; 
  31.         this.selectedImage = selectedImage; 
  32.         this.defaultColor = defaultColor; 
  33.         this.tintColor = tintColor; 
  34.         this.tabType = BarType.IMAGE_TEXT; 
  35.     } 

单个条目的封装

定义BottomBar,继承相对布局,实现之前定义的IBar接口,泛型就是每个条目所对应的实体类,由于目前并不知道泛型的具体类型,所以泛型直接使用问号来代替。BottomBar就是单个条目。

我们需要将component对象放入到BottomBar中,所以第二个参数传this,第三个参数为true。

  1. public class BottomBar extends DependentLayout implements IBar<BottomBarInfo<?>> { 
  2.  
  3.     /** 
  4.      * 当前条目所对应的数据 
  5.      */ 
  6.     private BottomBarInfo<Color> tabInfo; 
  7.     private Text mTabName; 
  8.     private Image mTabImage; 
  9.  
  10.     public BottomBar(Context context) { 
  11.         this(context, null); 
  12.     } 
  13.  
  14.     public BottomBar(Context context, AttrSet attrSet) { 
  15.         this(context, attrSet, ""); 
  16.     } 
  17.  
  18.     public BottomBar(Context context, AttrSet attrSet, String styleName) { 
  19.         super(context, attrSet, styleName); 
  20.         Component component = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_layout_bar_bottom, this, true); 
  21.         mTabImage = (Image) component.findComponentById(ResourceTable.Id_image); 
  22.         mTabName = (Text) component.findComponentById(ResourceTable.Id_name); 
  23.         mTabImage.setScaleMode(Image.ScaleMode.INSIDE); 
  24.     } 
  25.  
  26.     /** 
  27.      * 设置条目的数据 
  28.      * 
  29.      * @param data 
  30.      */ 
  31.     @Override 
  32.     public void setBarInfo(BottomBarInfo<?> data) { 
  33.         tabInfo = (BottomBarInfo<Color>) data; 
  34.         inflateInfo(falsetrue); 
  35.     } 
  36.  
  37.     /** 
  38.      * 初始化条目 
  39.      * 
  40.      * @param selected true 选中 
  41.      * @param init true 初始化 
  42.      */ 
  43.     private void inflateInfo(boolean selected, boolean init) { 
  44.         if (tabInfo.tabType == BottomBarInfo.BarType.IMAGE_TEXT) { 
  45.             if (init) { 
  46.                 // 图片和名称都可见 
  47.                 mTabName.setVisibility(VISIBLE); 
  48.                 mTabImage.setVisibility(VISIBLE); 
  49.                 if (!TextUtils.isEmpty(tabInfo.name)) { 
  50.                     // 设置条目的名称 
  51.                     mTabName.setText(tabInfo.name); 
  52.                 } 
  53.             } 
  54.             if (selected) { 
  55.                 // 显示选中的图片 
  56.                 mTabImage.setPixelMap(tabInfo.selectedImage); 
  57.                 mTabName.setTextColor(new Color(parseColor(tabInfo.tintColor))); 
  58.             } else { 
  59.                 // 显示未选中的图片 
  60.                 mTabImage.setPixelMap(tabInfo.defaultImage); 
  61.                 mTabName.setTextColor(new Color(parseColor(tabInfo.defaultColor))); 
  62.             } 
  63.         } else if (tabInfo.tabType == BottomBarInfo.BarType.IMAGE) { 
  64.             if (init) { 
  65.                 // 仅仅显示图片,将名称隐藏 
  66.                 mTabName.setVisibility(HIDE); 
  67.                 mTabImage.setVisibility(VISIBLE); 
  68.             } 
  69.             if (selected) { 
  70.                 // 显示选中的图片 
  71.                 mTabImage.setPixelMap(tabInfo.selectedImage); 
  72.             } else { 
  73.                 // 显示未选中的图片 
  74.                 mTabImage.setPixelMap(tabInfo.defaultImage); 
  75.             } 
  76.         } 
  77.     } 
  78.  
  79.     private int parseColor(Object color) { 
  80.         if (color instanceof String) { 
  81.             return Color.getIntColor((String) color); 
  82.         } else { 
  83.             return (int) color; 
  84.         } 
  85.     } 
  86.  
  87.     /** 
  88.      * 动态修改某个tab的高度 
  89.      * 
  90.      * @param height tab的高度 
  91.      */ 
  92.     @Override 
  93.     public void resetHeight(int height) { 
  94.         ComponentContainer.LayoutConfig config = getLayoutConfig(); 
  95.         config.height = height; 
  96.         setLayoutConfig(config); 
  97.         mTabName.setVisibility(HIDE); 
  98.     } 
  99.  
  100.     /** 
  101.      * 当某个条目被选中后的回调,该方法会被调用多次 
  102.      * 
  103.      * @param index 点击后选中条目的下标 
  104.      * @param preInfo 点击前选中的条目 
  105.      * @param nextInfo 点击后选中的条目 
  106.      */ 
  107.     @Override 
  108.     public void onBarSelectedChange(int index, BottomBarInfo<?> preInfo, BottomBarInfo<?> nextInfo) { 
  109.         if (nextInfo.tabType == BottomBarInfo.BarType.IMAGE) { 
  110.             // 当前条目的类型是IMAGE类型,则不做任何处理 
  111.             return
  112.         } 
  113.         if (preInfo == nextInfo) { 
  114.             // 假设当前选中的是条目1,同时点击的也是条目1,那就不需要做任何操作了 
  115.             return
  116.         } 
  117.         if (preInfo != tabInfo && nextInfo != tabInfo) { 
  118.             /** 
  119.              * 假设有三个条目,条目1、条目2、条目3,preInfo是条目1,nextInfo是条目3,tabInfo是条目2, 
  120.              * 点击前选中的是条目1,点击后选中的条目3,此时条目2就不需要做任何操作了 
  121.              */ 
  122.             return
  123.         } 
  124.         if (preInfo == tabInfo) { 
  125.             // 将点击前的条目反选 
  126.             inflateInfo(falsefalse); 
  127.         } else { 
  128.             // 选中被点击的条目 
  129.             inflateInfo(truefalse); 
  130.         } 
  131.     } 
  132.  
  133.     public BottomBarInfo<Color> getTabInfo() { 
  134.         return tabInfo; 
  135.     } 
  136.  
  137.     public Text getTabName() { 
  138.         return mTabName; 
  139.     } 
  140.  
  141.     public Image getImage() { 
  142.         return mTabImage; 
  143.     } 

底部导航栏的封装

定义BottomNavigationBar,继承栈布局。第一个泛型就是底部导航栏的条目,第二个泛型就是每个条目的数据。

  1. public class BottomNavigationBar extends StackLayout implements IBarLayout<BottomBar, BottomBarInfo<?>> { 
  2.  
  3.     private static final int ID_TAB_BOTTOM = 0XFF; 
  4.     /** 
  5.      * 事件监听的集合 
  6.      */ 
  7.     private List<OnBarSelectedListener<BottomBarInfo<?>>> tabSelectedListeners = new ArrayList<>(); 
  8.     /** 
  9.      * 当前选中的条目 
  10.      */ 
  11.     private BottomBarInfo<?> selectedInfo; 
  12.     /** 
  13.      * 底部导航栏的透明度 
  14.      */ 
  15.     private float barBottomAlpha = 1; 
  16.     /** 
  17.      * 底部导航栏的高度 
  18.      */ 
  19.     private float barBottomHeight = 50; 
  20.     /** 
  21.      * 底部导航栏线条的高度 
  22.      */ 
  23.     private float barBottomLineHeight = 0.5f; 
  24.     /** 
  25.      * 底部导航栏线条的颜色 
  26.      */ 
  27.     private RgbColor barBottomLineColor = new RgbColor(223, 224, 225); 
  28.     /** 
  29.      * 所有的tab 
  30.      */ 
  31.     private List<BottomBarInfo<?>> infoList; 
  32.  
  33.     public BottomNavigationBar(Context context) { 
  34.         this(context, null); 
  35.     } 
  36.  
  37.     public BottomNavigationBar(Context context, AttrSet attrSet) { 
  38.         this(context, attrSet, ""); 
  39.     } 
  40.  
  41.     public BottomNavigationBar(Context context, AttrSet attrSet, String styleName) { 
  42.         super(context, attrSet, styleName); 
  43.     } 
  44.  
  45.     /** 
  46.      * 根据数据查找条目 
  47.      * 
  48.      * @param info 条目的数据 
  49.      * @return 条目 
  50.      */ 
  51.     @Override 
  52.     public BottomBar findBar(BottomBarInfo<?> info) { 
  53.         ComponentContainer componentContainer = (ComponentContainer) findComponentById(ID_TAB_BOTTOM); 
  54.         for (int i = 0; i < componentContainer.getChildCount(); i++) { 
  55.             Component component = componentContainer.getComponentAt(i); 
  56.             if (component instanceof BottomBar) { 
  57.                 BottomBar bottomBar = (BottomBar) component; 
  58.                 if (bottomBar.getTabInfo() == info) { 
  59.                     return bottomBar; 
  60.                 } 
  61.             } 
  62.         } 
  63.         return null
  64.     } 
  65.  
  66.     /** 
  67.      * 添加监听 
  68.      * 
  69.      * @param listener 监听 
  70.      */ 
  71.     @Override 
  72.     public void addBarSelectedChangeListener(OnBarSelectedListener<BottomBarInfo<?>> listener) { 
  73.         tabSelectedListeners.add(listener); 
  74.     } 
  75.  
  76.     /** 
  77.      * 默认选中的条目 
  78.      * 
  79.      * @param defaultInfo 默认选中条目的信息 
  80.      */ 
  81.     @Override 
  82.     public void defaultSelected(BottomBarInfo<?> defaultInfo) { 
  83.         onSelected(defaultInfo); 
  84.     } 
  85.  
  86.     /** 
  87.      * 初始化所有的条目 
  88.      * 
  89.      * @param infoList 所有条目的信息 
  90.      */ 
  91.     @Override 
  92.     public void initInfo(List<BottomBarInfo<?>> infoList) { 
  93.         if (infoList == null || infoList.isEmpty()) { 
  94.             return
  95.         } 
  96.         this.infoList = infoList; 
  97.         // 移除之前已经添加的组件,防止重复添加 
  98.         removeComponent(); 
  99.         selectedInfo = null
  100.         // 添加背景 
  101.         addBackground(); 
  102.         // 添加条目 
  103.         addBottomBar(); 
  104.         // 添加线条 
  105.         addBottomLine(); 
  106.     } 
  107.  
  108.     /** 
  109.      * 添加线条 
  110.      */ 
  111.     private void addBottomLine() { 
  112.         Component line = new Component(getContext()); 
  113.         // 目前不支持直接设置背景颜色,只能通过Element来设置背景 
  114.         ShapeElement element = new ShapeElement(); 
  115.         element.setShape(ShapeElement.RECTANGLE); 
  116.         element.setRgbColor(barBottomLineColor); 
  117.         line.setBackground(element); 
  118.         LayoutConfig config = new LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT, 
  119.                 DisplayUtils.vp2px(getContext(), barBottomLineHeight)); 
  120.         // 位于底部 
  121.         config.alignment = LayoutAlignment.BOTTOM; 
  122.         config.setMarginBottom(DisplayUtils.vp2px(getContext(), barBottomHeight - barBottomLineHeight)); 
  123.         line.setAlpha(barBottomAlpha); 
  124.         addComponent(line, config); 
  125.     } 
  126.  
  127.     /** 
  128.      * 添加条目 
  129.      */ 
  130.     private void addBottomBar() { 
  131.         // 每个条目的宽度就是屏幕宽度除以条目的总个数 
  132.         int width = DisplayUtils.getDisplayWidthInPx(getContext()) / infoList.size(); 
  133.         // 高度是固定的值,这里需要做屏幕适配,将vp转换成像素 
  134.         int height = DisplayUtils.vp2px(getContext(), barBottomHeight); 
  135.         StackLayout stackLayout = new StackLayout(getContext()); 
  136.         stackLayout.setId(ID_TAB_BOTTOM); 
  137.         for (int i = 0; i < infoList.size(); i++) { 
  138.             BottomBarInfo<?> info = infoList.get(i); 
  139.             // 创建布局配置对象 
  140.             LayoutConfig config = new LayoutConfig(width, height); 
  141.             // 设置底部对齐 
  142.             config.alignment = LayoutAlignment.BOTTOM; 
  143.             // 设置左边距 
  144.             config.setMarginLeft(i * width); 
  145.             BottomBar bottomBar = new BottomBar(getContext()); 
  146.             tabSelectedListeners.add(bottomBar); 
  147.             // 初始化每个条目 
  148.             bottomBar.setBarInfo(info); 
  149.             // 添加条目 
  150.             stackLayout.addComponent(bottomBar, config); 
  151.             // 设置点击事件 
  152.             bottomBar.setClickedListener(component -> onSelected(info)); 
  153.         } 
  154.         LayoutConfig layoutConfig = new LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT, 
  155.                 ComponentContainer.LayoutConfig.MATCH_CONTENT); 
  156.         layoutConfig.alignment = LayoutAlignment.BOTTOM; 
  157.         addComponent(stackLayout, layoutConfig); 
  158.     } 
  159.  
  160.     /** 
  161.      * 点击条目后给外界回调 
  162.      * 
  163.      * @param nextInfo 点击后需要选中的条目 
  164.      */ 
  165.     private void onSelected(BottomBarInfo<?> nextInfo) { 
  166.         for (OnBarSelectedListener<BottomBarInfo<?>> listener : tabSelectedListeners) { 
  167.             listener.onBarSelectedChange(infoList.indexOf(nextInfo), selectedInfo, nextInfo); 
  168.         } 
  169.         if (nextInfo.tabType == BottomBarInfo.BarType.IMAGE_TEXT) { 
  170.             selectedInfo = nextInfo; 
  171.         } 
  172.     } 
  173.  
  174.     /** 
  175.      * 添加背景 
  176.      */ 
  177.     private void addBackground() { 
  178.         Component component = new Component(getContext()); 
  179.         // 目前还不能直接设置背景颜色,只能通过Element来设置背景 
  180.         ShapeElement element = new ShapeElement(); 
  181.         element.setShape(ShapeElement.RECTANGLE); 
  182.         RgbColor rgbColor = new RgbColor(255, 255, 255); 
  183.         element.setRgbColor(rgbColor); 
  184.         component.setBackground(element); 
  185.         component.setAlpha(barBottomAlpha); 
  186.         LayoutConfig config = new LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT, 
  187.                 DisplayUtils.vp2px(getContext(), barBottomHeight)); 
  188.         config.alignment = LayoutAlignment.BOTTOM; 
  189.         addComponent(component, config); 
  190.     } 
  191.  
  192.     /** 
  193.      * 移除之前已经添加的组件,防止重复添加 
  194.      */ 
  195.     private void removeComponent() { 
  196.         for (int i = getChildCount() - 1; i > 0; i--) { 
  197.             removeComponentAt(i); 
  198.         } 
  199.         tabSelectedListeners.removeIf(listener -> 
  200.                 listener instanceof BottomBar); 
  201.     } 
  202.  
  203.     /** 
  204.      * 设置底部导航栏的透明度 
  205.      * 
  206.      * @param barBottomAlpha 底部导航栏的透明度 
  207.      */ 
  208.     public void setBarBottomAlpha(float barBottomAlpha) { 
  209.         this.barBottomAlpha = barBottomAlpha; 
  210.     } 
  211.  
  212.     /** 
  213.      * 设置底部导航栏的高度 
  214.      * 
  215.      * @param barBottomHeight 底部导航栏的高度 
  216.      */ 
  217.     public void setBarBottomHeight(float barBottomHeight) { 
  218.         this.barBottomHeight = barBottomHeight; 
  219.     } 
  220.  
  221.     /** 
  222.      * 设置底部导航栏线条的高度 
  223.      * 
  224.      * @param barBottomLineHeight 底部导航栏线条的高度 
  225.      */ 
  226.     public void setBarBottomLineHeight(float barBottomLineHeight) { 
  227.         this.barBottomLineHeight = barBottomLineHeight; 
  228.     } 
  229.  
  230.     /** 
  231.      * 设置底部导航栏线条的颜色 
  232.      * 
  233.      * @param barBottomLineColor 底部导航栏线条的颜色 
  234.      */ 
  235.     public void setBarBottomLineColor(RgbColor barBottomLineColor) { 
  236.         this.barBottomLineColor = barBottomLineColor; 
  237.     } 

initInfo(List<BottomBarInfo<?>> infoList)该方法由外界调用,外界将所有的条目信息传递过来,我们将条目添加到底部导航栏。首先移除之前已经添加的组件,防止重复添加,然后添加背景,添加条目,添加线条。

更多原创,请关注:软通动力HarmonyOS学院https://harmonyos.51cto.com/column/30

想了解更多内容,请访问:

51CTO和华为官方合作共建的鸿蒙技术社区

https://harmonyos.51cto.com

 

责任编辑:jianghua 来源: 鸿蒙社区
相关推荐

2021-02-24 15:22:47

鸿蒙HarmonyOS应用开发

2021-03-01 09:48:24

鸿蒙HarmonyOS应用开发

2021-01-13 09:40:31

鸿蒙HarmonyOS开发

2021-02-04 13:06:38

鸿蒙HarmonyOS应用开发

2021-02-26 14:15:27

鸿蒙HarmonyOS应用开发

2021-06-28 14:48:03

鸿蒙HarmonyOS应用

2021-01-18 09:52:20

鸿蒙HarmonyOS开发

2021-02-04 09:45:19

鸿蒙HarmonyOS应用开发

2021-01-12 12:04:40

鸿蒙HarmonyOS应用开发

2021-01-20 09:54:56

鸿蒙HarmonyOS开发

2021-01-21 13:21:18

鸿蒙HarmonyOSPhotoview组件

2021-03-01 14:01:41

鸿蒙HarmonyOS应用开发

2021-01-22 17:33:03

鸿蒙HarmonyOS应用开发

2021-05-12 15:17:39

鸿蒙HarmonyOS应用

2021-03-31 09:50:25

鸿蒙HarmonyOS应用开发

2021-04-12 09:36:54

鸿蒙HarmonyOS应用

2021-03-19 17:42:01

鸿蒙HarmonyOS应用开发

2021-04-20 09:42:20

鸿蒙HarmonyOS应用开发

2021-08-04 14:16:41

鸿蒙HarmonyOS应用

2021-03-10 15:03:40

鸿蒙HarmonyOS应用
点赞
收藏

51CTO技术栈公众号