長沙Java培訓機構課件:HarmonyOS快速入門

千鋒雲端計算發表於2021-11-30

       近期華為的鴻蒙系統(HarmonyOS)在各大平臺瘋狂刷屏,可謂是出盡了風頭,很多小夥伴都問有沒有鴻蒙相關的開發教程,那麼它來了!從今天起小編要更新一套千鋒 講師主講的有關鴻蒙系統開發的系列教程。本篇文章講解一下HarmonyOS快速入門:

1、建立xml佈局檔案

第一個頁面內有一個文字和一個按鈕,使用DependentLayout佈局,通過Text和Button元件來實現,其中vp和fp分別表示虛擬畫素和字型畫素。“ability_main.xml”的示例程式碼如下:

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

<DependentLayout

xmlns: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”,單擊Enter鍵。

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

<shape

xmlns: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


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69916964/viewspace-2844981/,如需轉載,請註明出處,否則將追究法律責任。

相關文章