<option id="mwy0y"><strong id="mwy0y"></strong></option>
  • <ul id="mwy0y"><sup id="mwy0y"></sup></ul>
  • <ul id="mwy0y"></ul>
  • <del id="mwy0y"><dfn id="mwy0y"></dfn></del><ul id="mwy0y"><sup id="mwy0y"></sup></ul>
  • <abbr id="mwy0y"></abbr>

    千鋒教育-做有情懷、有良心、有品質(zhì)的職業(yè)教育機(jī)構(gòu)

    400-811-9990
    手機(jī)站
    千鋒教育

    千鋒學(xué)習(xí)站 | 隨時(shí)隨地免費(fèi)學(xué)

    千鋒教育

    掃一掃進(jìn)入千鋒手機(jī)站

    領(lǐng)取全套視頻
    千鋒教育

    關(guān)注千鋒學(xué)習(xí)站小程序
    隨時(shí)隨地免費(fèi)學(xué)習(xí)課程

    上海
    • 北京
    • 鄭州
    • 武漢
    • 成都
    • 西安
    • 沈陽
    • 廣州
    • 南京
    • 深圳
    • 大連
    • 青島
    • 杭州
    • 重慶
    當(dāng)前位置:長沙千鋒IT培訓(xùn)  >  技術(shù)要點(diǎn)  >  千鋒長沙Java培訓(xùn):HarmonyOS快速入門

    千鋒長沙Java培訓(xùn):HarmonyOS快速入門

    來源:千鋒教育
    發(fā)布人:千鋒長沙
    時(shí)間: 2021-11-30 16:15:23

           近期華為的鴻蒙系統(tǒng)(HarmonyOS)在各大平臺(tái)瘋狂刷屏,可謂是出盡了風(fēng)頭,很多小伙伴都問有沒有鴻蒙相關(guān)的開發(fā)教程,那么它來了!從今天起小編要更新一套千鋒長沙Java培訓(xùn)講師主講的有關(guān)鴻蒙系統(tǒng)開發(fā)的系列教程。本篇文章講解一下HarmonyOS快速入門:

    1、創(chuàng)建xml布局文件

    第一個(gè)頁面內(nèi)有一個(gè)文本和一個(gè)按鈕,使用DependentLayout布局,通過Text和Button組件來實(shí)現(xiàn),其中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>

    按鈕的背景是藍(lán)色膠囊樣式,可以通過graphic目錄下的XML文件來設(shè)置。

    右鍵點(diǎn)擊“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、創(chuàng)建主程序

    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);

    // 點(diǎn)擊按鈕跳轉(zhuǎn)至第二個(gè)頁面

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

    }

    @Override

    public void onActive() {

    super.onActive();

    }

    @Override

    public void onForeground(Intent intent) {

    super.onForeground(intent);

    }

    }

    3、創(chuàng)建另一個(gè)界面

    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);

    //設(shè)置布局的寬度和高度

    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);

    // 設(shè)置文本的布局

    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、運(yùn)行結(jié)果如下

    QQ截圖20211130161404

    聲明:本站稿件版權(quán)均屬千鋒教育所有,未經(jīng)許可不得擅自轉(zhuǎn)載。

    猜你喜歡LIKE

    最新文章NEW

    相關(guān)推薦HOT

    更多>>

    快速通道 更多>>

    最新開班信息 更多>>

    網(wǎng)友熱搜 更多>>