步骤如下:
①在项目上新建 module,选择 libiary 创建鸿蒙组件
②在项目中导入组件
lg\entry\build.gradle 文件中的: dependencies { implementation fileTree(dir: libs, include: [.jar, .har]) testImplementation junit:junit:4.13 ohosTestImplementation com.huawei.ohos.testkit:runner:1.0.0.100 下面是导入的组件名 与settings.gradle 名字一样 implementation project(“:BottomNavigationF”) }
③在刚才创建的 modul 编写组件类 package com.llt.bottomnavigationf;
import ohos.agp.components.AttrSet; import ohos.agp.components.Component; import ohos.agp.components.DirectionalLayout; import ohos.app.Context;
/*键盘alt + insert constructor 实现这里面的方法 */ public class BottomNavigationBar extends DirectionalLayout {
public BottomNavigationBar(Context context) { super(context); } public BottomNavigationBar(Context context, AttrSet attrSet) { super(context, attrSet); } public BottomNavigationBar(Context context, AttrSet attrSet, String styleName) { super(context, attrSet, styleName); }}
④在项目中的 xml 文件引入 <?xml version=”1.0″ encoding=”utf-8″?> <DirectionalLayout xmlns:ohos=”http://schemas.huawei.com/res/ohos” ohos:height=”match_parent” ohos:width=”match_parent” ohos:alignment=”center” ohos:orientation=”vertical”>
<com.llt.bottomnavigationf.BottomNavigationBar ohos:height=”300vp” ohos:width=”match_parent” ohos:background_element=”#c48c48″
<Text ohos:height=”match_content” ohos:width=”match_content” ohos:text_color=”#fff” ohos:text_size=”28fp” ohos:text=”Text”
123123 </com.llt.bottomnavigationf.BottomNavigationBar>
项目地址:
https://gitee.com/blueskyliu/lg.git
免责声明:文章内容来自互联网,本站不对其真实性负责,也不承担任何法律责任,如有侵权等情况,请与本站联系删除。
转载请注明出处:鸿蒙OS自定义组件实现! https://www.yhzz.com.cn/a/12101.html