<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          HarmonyOS學習路之開發(fā)基礎—入門篇(Hello,World)

          共 5390字,需瀏覽 11分鐘

           ·

          2021-06-26 23:18

          點擊上方藍色字體,關注我們


          1


          編寫第一個頁面



          在Java UI框架中,提供了兩種編寫布局的方式:在XML中聲明UI布局和在代碼中創(chuàng)建布局。這兩種方式創(chuàng)建出的布局沒有本質差別,為了熟悉兩種方式,我們將通過XML的方式編寫第一個頁面,通過代碼的方式編寫第二個頁面。

          1、在“Project”窗口,點擊“entry > src > main > resources > base > layout”,打開“ability_main.xml”文件。



          2、第一個頁面內有一個文本和一個按鈕,使用DependentLayout布局,通過Text和Button組件來實現,其中vp和fp分別表示虛擬像素和字體像素?!癮bility_main.xml”的示例代碼如下:


          <?xml version="1.0" encoding="utf-8"?>
          <DependentLayout
              xmlns:ohos="http://schemas.huawei.com/res/ohos"
              ohos:width="match_parent"
              ohos:height="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:margin="10vp"/>

          </DependentLayout>


          3、按鈕的背景是藍色膠囊樣式,可以通過graphic目錄下的XML文件來設置。右鍵點擊“graphic”文件夾,選擇“New > File”,命名為“background_button.xml”,單擊回車鍵。



          “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>


          在layout目錄下的“ability_main.xml”文件中,使用background_element="$graphic:background_button"的方式引用“background_button.xml”文件:


          <?xml version="1.0" encoding="utf-8"?>
          <DependentLayout
              ...
              <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:margin="10vp"
                  ohos:background_element="$graphic:background_button"/>

          </DependentLayout>


          4、在XML文件中添加組件后,需要在Java代碼中加載XML布局。在“Project”窗口,選擇“entry > src > main > java > com.example.myapplication > slice” ,打開“MainAbilitySlice.java”文件,使用setUIContent方法加載“ability_main.xml”布局。


          “MainAbilitySlice.java”的示例代碼如下:


          package com.example.firstdemo.slice;

          import com.example.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);
              }

              @Override
              public void onActive() {
                  super.onActive();
              }

              @Override
              public void onForeground(Intent intent) {
                  super.onForeground(intent);
              }
          }


          5、使用預覽器或模擬器運行項目,效果如下圖所示:



          往期推薦



          點擊閱讀原文,更精彩~
          瀏覽 90
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  久久精品成人毛片 | 逼视频在线观看 | 人人操人人爱超碰 | 操逼视频,国产操逼片 | 日本一级黄色作爱视频网站 |