HarmonyOS快速入门

       近期华为的鸿蒙系统(HarmonyOS)在各大平台疯狂刷屏,可谓是出尽了风头,很多小伙伴都问有没有鸿蒙相关的开发教程,那么它来了!从今天起小编要更新一套长沙Java培训讲师主讲的有关鸿蒙系统开发的系列教程。本篇文章讲解一下HarmonyOS快速入门:

1、创建xml布局文件

第一个页面内有一个文本和一个按钮,使用DependentLayout布局,通过Text和Button组件来实现,其中vp和fp分别表示虚拟像素和字体像素。“ability_main.xml”的示例代码如下:

<?xml version="1.0" encoding="utf-8"?>

<DependentLayout

xmlns:ohos="http://schemas.huawei.com/res/ohos"

ohos:height="match_parent"

ohos:width="match_parent">

<Text

ohos:id="$+id:text"

ohos:width="match_content"

ohos:height="match_content"

ohos:text="Hello World"

ohos:text_color="#000000"

ohos:text_size="32fp"

ohos:center_in_parent="true"/>

<Button

ohos:id="$+id:button"

ohos:width="match_content"

ohos:height="match_content"

ohos:text="Next"

ohos:text_size="19fp"

ohos:text_color="#FFFFFF"

ohos:top_padding="8vp"

ohos:bottom_padding="8vp"

ohos:right_padding="70vp"

ohos:left_padding="70vp"

ohos:center_in_parent="true"

ohos:below="$id:text"

ohos:background_element="$graphic:background_button"

ohos:margin="10vp"/>

</DependentLayout>

按钮的背景是蓝色胶囊样式,可以通过graphic目录下的XML文件来设置。

右键点击“graphic”文件夹,选择“New > File”,命名为“background_button.xml”,单击回车键。

<?xml version="1.0" encoding="utf-8"?>

<shape

xmlns:ohos="http://schemas.huawei.com/res/ohos"

ohos:shape="rectangle">

<corners

ohos:radius="100"/>

<solid

ohos:color="#007DFF"/>

</shape>

2、创建主程序

package com.sudojava.firstdemo.slice;

import com.sudojava.firstdemo.ResourceTable;

import ohos.aafwk.ability.AbilitySlice;

import ohos.aafwk.content.Intent;

public class MainAbilitySlice extends AbilitySlice {

@Override

public void onStart(Intent intent) {

super.onStart(intent);

super.setUIContent(ResourceTable.Layout_ability_main);

Button button = (Button) findComponentById(ResourceTable.Id_button);

// 点击按钮跳转至第二个页面

button.setClickedListener(listener -> present(new SecondAbilitySlice(), new Intent()));

}

@Override

public void onActive() {

super.onActive();

}

@Override

public void onForeground(Intent intent) {

super.onForeground(intent);

}

}

3、创建另一个界面

package com.sudojava.firstdemo.slice;

import com.sudojava.firstdemo.MainAbility;

import ohos.aafwk.ability.AbilitySlice;

import ohos.aafwk.content.Intent;

import ohos.agp.colors.RgbColor;

import ohos.agp.components.DependentLayout;

import ohos.agp.components.Text;

import ohos.agp.components.element.ShapeElement;

import ohos.agp.utils.Color;

public class SecondAbilitySlice extends AbilitySlice {

@Override

protected void onStart(Intent intent) {

super.onStart(intent);

//声明布局

DependentLayout layout = new DependentLayout(this);

//设置布局的宽度和高度

layout.setWidth(DependentLayout.LayoutConfig.MATCH_PARENT);

layout.setHeight(DependentLayout.LayoutConfig.MATCH_CONTENT);

ShapeElement shapeElement = new ShapeElement();

shapeElement.setRgbColor(new RgbColor(255,255,255));

layout.setBackground(shapeElement);

Text text = new Text(this);

text.setText("Hi Here");

text.setWidth(DependentLayout.LayoutConfig.MATCH_PARENT);

text.setTextSize(100);

text.setTextColor(Color.BLACK);

// 设置文本的布局

DependentLayout.LayoutConfig textConfig = new DependentLayout.LayoutConfig(DependentLayout.LayoutConfig.MATCH_CONTENT, DependentLayout.LayoutConfig.MATCH_CONTENT);

textConfig.addRule(DependentLayout.LayoutConfig.CENTER_IN_PARENT);

text.setLayoutConfig(textConfig);

layout.addComponent(text);

super.setUIContent(layout);

}

}

4、运行结果如下

QQ截图20211130161404

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值