鸿蒙OS(二)步步深入

    科技2024-05-22  94

    今天我们继续聊鸿蒙OS!

     

     

    01

    生命周期

     

    在HelloWorld中我们默认的项目已经给我们创建上了默认的代码,我们发现其会自动重写一个onStart()的方法,与我们Android开发中onCreated()略有不同,看来鸿蒙OS也是有不同的生命周期的。

     

    onStart()

     

    当系统首次创建Ability实例时,触发该方法的回调。对于一个Ability实例,该回调在其生命周期过程中仅触发一次,方法执行完毕之后进入INACTIVE状态。在开发过程中必须重写该方法并配置展示的内容及其它的一些操作。

    该方法类似于Android中的onCreated()方法。

     

    onActive()

     

    Ability会在进入INACTIVE状态后来到前台,之后系统回调该方法。之后Ability进入ACTIVE状态,该状态是展示到屏幕中,获取到焦点,与用户交互的状态。当用户点击返回键或跳转到其他的Ability中,之后Ability又会回到INACTIVE状态,系统会回调onInactive()。

    该方法类似于Android中的onResume()生命周期。

     

    onInactive()

     

    当Ability失去焦点时,系统会回调此方法。之后系统进入INACTIVE状态,平时开发中通过灵活使用该方法实现页面失去焦点时表现得恰当的一些行为。

    该方法类似于Android中的onPause()和onStop()。

     

    onBackground()

     

    当Ability对用户不可见时,系统将回调该方法并进行一些资源的释放,之后Ability进入BACKGROUND状态。平时使用时要在该方法中进行资源的释放或者一些耗时状态的保存。

     

    onForeground()

     

    当处于BACKGROUND状态的Ability仍驻留在内存中时,重新回到前台时系统将先调用该方法回调,之后Ability生命周期回到INACTIVE状态,需要在该方法中重新申请onBackground()中释放的资源,之后Ability生命周期进入到ACTIVE状态。

     

    onStop()

     

    系统要销毁时,将会触发该函数,通知用户进行资源的释放。

    该生命周期类似于Android中的onDestory()。

     

     

    来张图感觉更清晰一些~~~

     

     

    02

    布局

     

     以往我们在Android中开发时会自动生成一个res文件,并在res目录下生成layout和drawable等相关资源文件,但是在DevEco中并不会自动生成layout文件,相比Android在xml中,DevEco提供了两种编写布局的方式。

     

    在代码中创建布局

     

    用代码创建Component和ComponentContainer对象,为这些对象设置合适的布局参数和属性值,并将Component添加到ComponterContainer中,从而创建出完整的界面。

     

    @Overridepublic void onStart(Intent intent) { super.onStart(intent); // 步骤1 声明布局 DirectionalLayout directionalLayout = new DirectionalLayout(context); // 步骤2 设置布局大小 directionalLayout.setWidth(ComponentContainer.LayoutConfig.MATCH_PARENT); directionalLayout.setHeight(ComponentContainer.LayoutConfig.MATCH_PARENT); // 步骤3 设置布局属性及ID(ID视需要设置即可) directionalLayout.setOrientation(Component.VERTICAL); directionalLayout.setPadding(32, 32, 32, 32); Text text = new Text(context); text.setText("My name is Text."); text.setTextSize(50); text.setId(100); // 步骤4.1 为组件添加对应布局的布局属性 DirectionalLayout.LayoutConfig layoutConfig = new DirectionalLayout.LayoutConfig(LayoutConfig.MATCH_CONTENT, LayoutConfig.MATCH_CONTENT); layoutConfig.alignment = LayoutAlignment.HORIZONTAL_CENTER; text.setLayoutConfig(layoutConfig); // 步骤4.2 将Text添加到布局中 directionalLayout.addComponent(text); // 类似的添加一个Button Button button = new Button(context); layoutConfig.setMargins(0, 50, 0, 0); button.setLayoutConfig(layoutConfig); button.setText("My name is Button."); button.setTextSize(50); button.setId(100); ShapeElement background = new ShapeElement(); background.setRgbColor(new RgbColor(0, 125, 255)); background.setCornerRadius(25); button.setBackground(background); button.setPadding(10, 10, 10, 10); button.setClickedListener(new Component.ClickedListener() { @Override // 在组件中增加对点击事件的检测 public void onClick(Component Component) { // 此处添加按钮被点击需要执行的操作 } }); directionalLayout.addComponent(button); // 步骤5 将布局作为根布局添加到视图树中 super.setUIContent(directionalLayout);}

     

    在XML中创建布局:按层级结构来描述Component和ComponentContainer的关系,给组件设定合适的布局参数和属性值,代码中直接加载生成布局。

     

    在DevEco Studio编译器中的Project的窗口中,打开entry>src>main>resources>base文件夹下右键base文件夹,选择New>Directory,命名为layout。

     

    右键layout文件夹,选择New>File,命名为main_layout.xml

     

    打开新建的布局文件,修改文件中的内容

     

    <?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:orientation="vertical" ohos:padding="32"> <Text ohos:id="$+id:text" ohos:width="match_content" ohos:height="match_content" ohos:layout_alignment="horizontal_center" ohos:text="My name is Text." ohos:text_size="25vp"/> <Button ohos:id="$+id:button" ohos:width="match_content" ohos:height="match_content" ohos:layout_alignment="horizontal_center" ohos:text="My name is Button." ohos:text_size="50"/></DirectionalLayout>

     

    在代码中加载XML布局,并添加韦根布局或作为其他布局的子Component。

     

    @Overridepublic void onStart(Intent intent) { super.onStart(intent); // 加载XML布局作为根布局 super.setUIContent(ResourceTable.Layout_main_layout); // 查找布局中组件 Button button = (Button) findComponentById(ResourceTable.Id_button); if (button != null) { // 设置组件的属性 ShapeElement background = new ShapeElement(); background.setRgbColor(new RgbColor(0,125,255)); background.setCornerRadius(25); button.setBackground(background); button.setClickedListener(new Component.ClickedListener() { @Override // 在组件中增加对点击事件的检测 public void onClick(Component Component) { // 此处添加按钮被点击需要执行的操作 } }); }}

     

    布局种类

     

    DirectionalLayout

     

    将用于一组组件按水平或者垂直方向排布,能够方面的对齐布局内的组件。该布局等同于Android中的LinearLayout,布局中的属性基本上都一致。

     

     

    水平属性:

    <DirectionalLayout xmlns:ohos="http://schemas.huawei.com/res/ohos" ohos:width="match_parent" ohos:height="match_content"    ohos:orientation="horizontal"/>

     

    垂直属性:

    <DirectionalLayout xmlns:ohos="http://schemas.huawei.com/res/ohos" ohos:width="match_parent" ohos:height="match_content"    ohos:orientation="vertical"/>

     

    DependentLayout

     

    与DirectionalLayout相比,拥有更多的排布方式,每个组件可以指定相对于其他同级元素的位置或者指定相对于父组件的位置。该布局与Android中的RelativeLayout布局一样,基本属性都一致。

     

      

     

    <DependentLayout xmlns:ohos="http://schemas.huawei.com/res/ohos" ohos:width="match_content" ohos:height="match_content" ohos:background_element="$graphic:color_light_gray_element"> <Text ohos:id="$+id:text1" ohos:width="match_content" ohos:height="match_content" ohos:left_margin="15vp" ohos:top_margin="15vp" ohos:bottom_margin="15vp" ohos:text="text1" ohos:text_size="20fp" ohos:background_element="$graphic:color_cyan_element"/> <Text ohos:id="$+id:text2" ohos:width="match_content" ohos:height="match_content" ohos:left_margin="15vp" ohos:top_margin="15vp" ohos:right_margin="15vp" ohos:bottom_margin="15vp" ohos:text="end_of text1" ohos:text_size="20fp" ohos:background_element="$graphic:color_cyan_element" ohos:end_of="$id:text1"/></DependentLayout>

     

     

    03

    组件

     

    组件类型主要组件基础组件text、image、progress、rating、span、marquee、image-animator、divider、search、menu、chart容器组件div、list、list-item、stack、swiper、tabs、tab-bar、tab-content、popup、list-item-group、refresh、dialog媒体组件video画布组件canvas

     

     

    04

    总结

     

    本文从Ability的生命周期,再去了解布局编写方式等,到这里基本上完成一个简单Demo的还是很简单的,感觉鸿蒙OS的开发与Android开发基本上还是差不多的。

    Processed: 0.013, SQL: 8