千锋教育-做有情怀、有良心、有品质的职业教育机构

400-811-9990
手机站
千锋教育

千锋学习站 | 随时随地免费学

千锋教育

扫一扫进入千锋手机站

领取全套视频
千锋教育

关注千锋学习站小程序
随时随地免费学习课程

上海
  • 北京
  • 郑州
  • 武汉
  • 成都
  • 西安
  • 沈阳
  • 广州
  • 南京
  • 深圳
  • 大连
  • 青岛
  • 杭州
  • 重庆
当前位置:长沙千锋IT培训  >  技术要点  >  千锋长沙Java培训:HarmonyOS快速入门

千锋长沙Java培训:HarmonyOS快速入门

来源:千锋教育
发布人:千锋长沙
时间: 2021-11-30 16:15:23

       近期华为的鸿蒙系统(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

声明:本站稿件版权均属千锋教育所有,未经许可不得擅自转载。

猜你喜欢LIKE

最新文章NEW

相关推荐HOT

更多>>

快速通道 更多>>

最新开班信息 更多>>

网友热搜 更多>>