鸿蒙技术论坛,鸿蒙应用开发入门(三):开发第一个鸿蒙应用

目录:

3.1 第一个鸿蒙应用实现需求

编写两张页面,实现在第一张页面点击按钮跳转到第二张页面。在Java UI框架中,提供了两种编写布局的方式:在XML中声明UI布局和在代码中创建布局。这两种方式创建出的布局没有本质差别,都是我们需要熟悉方式,所以我们将通过XML的方式布局第一张页面,然后再通过代码的方式布局第二张页面。

3.2 用XML布局第一张页面

1. 打开layout下面的“ability_main.xml”文件

2. 在“ability_main.xml”文件中创建一个文本和一个按钮

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

ohos:width="match_parent"

ohos:height="match_parent"

ohos:background_element="#000000">

ohos:id="$+id:text"

ohos:width="match_content"

ohos:height="match_content"

ohos:text="Hello World"

ohos:text_color="white"

ohos:text_size="32fp"

ohos:center_in_parent="true"/>

ohos:id="$+id:button"

ohos:width="match_content"

ohos:height="match_content"

ohos:text="Next"

ohos:text_size="19fp"

ohos:text_color="white"

ohos:top_padding="8vp"

ohos:bottom_padding="8vp"

ohos:right_padding="80vp"

ohos:left_padding="80vp"

ohos:background_element="$graphic:background_button"

ohos:below="$id:text"

ohos:horizontal_center="true"

/>

3. 创建按钮的背景

按钮的背景是通过“background_button”来指定的。右键点击“graphic”文件夹,选择“New > File”,命名为“background_button.xml”。

3.3 用编程的方式布局第二张页面

1. 创建Feature Ability

2. 代码编写界面

public class SecondAbilitySlice extends AbilitySlice {

@Override

public void onStart(Intent intent) {

super.onStart(intent);

// 声明布局

DependentLayout myLayout = new DependentLayout(this);

// 设置页面布局大小和背景色

myLayout.setWidth(MATCH_PARENT);

myLayout.setHeight(MATCH_PARENT);

ShapeElement element = new ShapeElement();

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

myLayout.setBackground(element);

// 创建一个文本

Text text = new Text(this);

text.setText("Nice to meet you.");

text.setTextSize(55);

text.setTextColor(Color.BLACK);

// 设置文本的布局

DependentLayout.LayoutConfig textConfig =

new DependentLayout.LayoutConfig(MATCH_CONTENT,MATCH_CONTENT);

textConfig.addRule(DependentLayout.LayoutConfig.CENTER_IN_PARENT);

text.setLayoutConfig(textConfig);

myLayout.addComponent(text);

super.setUIContent(myLayout);

}

@Override

public void onActive() {

super.onActive();

}

@Override

public void onForeground(Intent intent) {

super.onForeground(intent);

}

}

作者:zhonghongfa

想了解更多内容,请访问: 51CTO和华为官方战略合作共建的鸿蒙技术社区https://harmonyos.51cto.com

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值