目錄:
HarmonyOS第一個應用項目
一、HarmonyOS應用項目結構
1.項目結構圖
如上圖:
FirstApp為項目名稱
External Libraries為一些擴展庫或者第三方庫,默認就有不需要進行加載
2.項目結構圖
gradle:**Gradle**配置文件,由系統自動生成,一般情況下不需要進行修改。
build: 為編譯后生成文件,如最后生成的hap文件(entry-debug-unsigned.hap)
entry:非常重要,一個項目有且只有一個entry,默認啟動模塊(主模塊),開發者用於編寫源碼文件以及開發資源文件的目錄
gradle:gradle jar包以及版本信息,可以修改gradle版本,最好使用默認配置
build.gradle 項目編譯配置文件以及第三方庫引用等
gradle.properties gradle屬性值,默認是空
gradlew 是linux gradle命令腳本
gradlew.bat是windows gradle命令腳本
local.properties 應用本地屬性配置文件,目前只有兩個,主要是SDK和nodejs的安裝
3.Entry目錄結構
libs:用於存放entry模塊的依賴文件。
src :添加源碼的地方
main 是主要重要的關注的地方,以后就在這個目錄暢享你的未來了
java:用於存放Java源碼。
resources用於存放應用所用到的資源文件,如圖形、多媒體、字符串、布局文件等
test 編寫代碼單元測試代碼的目錄
config.json:HAP清單文件,詳細說明請參考config.json配置文件介紹。
.gitignore:標識git版本管理需要忽略的文件。
build.gradle:entry模塊的編譯配置文件。
二、第一個HarmonyOS應用
1.項目說明
1.實現一個XML文件布局(布局中包含一個Button和一個Text組件)
2.用代碼實現一個頁面布局(布局中包含一個Button和一個Text組件)
3.點擊Button可以實現兩個界面跳轉
2.XML布局文件
1.頁面布局文件
<?xml version="1.0" encoding="utf-8"?>
<DependentLayout
xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:background_element="#5555FF55">
<Text
ohos:id="$+id:text_id"
ohos:height="match_content"
ohos:width="match_content"
ohos:text="HarmonyOS第一個XML布局實例"
ohos:text_size="22fp"
ohos:left_padding="5vp"
ohos:right_padding="5vp"
ohos:center_in_parent="true"
ohos:background_element="$graphic:background_text"
/>
<Button
ohos:id="$+id:button_id"
ohos:width="match_content"
ohos:height="match_content"
ohos:text="下個頁面"
ohos:text_size="25fp"
ohos:top_padding="5vp"
ohos:bottom_padding="5vp"
ohos:left_padding="5vp"
ohos:right_padding="5vp"
ohos:below="$id:text_id"
ohos:center_in_parent="true"
ohos:background_element="$graphic:background_button"/>
</DependentLayout>
組件常用的屬性
ohos:height="match_parent" // 組件的高度
ohos:width="match_parent" //組件的寬度
ohos:orientation="vertical" //布局組件的方向屬性 適用於DirectionalLayout布局
ohos:id="$+id:text_helloworld" //組件ID,常用Button、Text等組件,用於標識唯一一個組件,便於查找
ohos:text="Hello World" //組件上顯示的文本內容,如Button名字或者Text的標簽或者文本
ohos:text_size="50fp"
fp:寬度或者高度,距離離,一般描述組件自己的大小(與屏幕密度有關系)
px:描述像素
vp: 寬度或者高度,距離離,一般描述間隔,如內間距,外間距等(與屏幕密度有關系)
ohos:text_color="#55FFFFFF" //文本顏色,55設置透明度, FFFFFF設置的RGB顏色值
ohos:top_padding="8vp" //設置上間距
ohos:bottom_padding="8vp"//設置下間距
ohos:left_padding="8vp"//設置左邊距
ohos:right_padding="8vp"//設置右邊距
ohos:below="$id:text_helloworld" //設置在文本組件的下方 用於在相對布局中
2.背景布局文件
background_text
<?xml version="1.0" encoding="UTF-8" ?>
<shape xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:shape="rectangle">
<solid
ohos:color="#ff888888"/>
<corners
ohos:radius="20"/>
</shape>
background_button
<?xml version="1.0" encoding="UTF-8" ?>
<shape xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:shape="rectangle">
<solid
ohos:color="#FF007DFF"/>
<corners
ohos:radius="20"/>
</shape>
ohos:shape="rectangle"//背景的圖形為矩形
ohos:color="#FF007DFF"//背景的圖形顏色
ohos:radius="20"//背景的圖形角的弧度
3.Code布局實現
1.代碼實現布局加載
/*創建一個布局,布局中包含一個Text和Button組件*/
/*申明布局並設置布局的大小和背景顏色*/
DependentLayout secondLayout = new DependentLayout(this);
secondLayout.setHeight(MATCH_PARENT);
secondLayout.setWidth(MATCH_PARENT);
ShapeElement element = new ShapeElement();
element.setRgbColor(new RgbColor(55,55,55));
secondLayout.setBackground(element);
/*創建一個Text文本*/
Text text = new Text(this);
text.setText("HarmonyOS第二個頁面");
text.setTextSize(20, Text.TextSizeType.FP);
text.setTextColor(Color.CYAN);
text.setId(TEXT_ID);
/*創建一個Button 按鈕*/
Button button = new Button(this);
button.setText("上個頁面");
button.setTextSize(25, Text.TextSizeType.FP);
button.setTextColor(Color.RED);
button.setPadding(10,10,10,10);
button.setId(BUTTON_ID);
ShapeElement element1 = new ShapeElement();
element1.setRgbColor(new RgbColor(255,55,255));
button.setBackground(element1);
/*設置布局參數*/
DependentLayout.LayoutConfig secondLayoutConfig =
new DependentLayout.LayoutConfig(MATCH_CONTENT,MATCH_CONTENT);
secondLayoutConfig.addRule(DependentLayout.LayoutConfig.CENTER_IN_PARENT);
text.setLayoutConfig(secondLayoutConfig);
secondLayoutConfig.addRule(DependentLayout.LayoutConfig.BELOW, TEXT_ID);
button.setLayoutConfig(secondLayoutConfig);
secondLayout.addComponent(text);
secondLayout.addComponent(button);
/*將布局添加到Ability*/
super.setUIContent(secondLayout);
4.Button按鍵事件與界面跳轉
1.XML頁面Button
/*獲取Button 組件*/
Button button = (Button) findComponentById(ResourceTable.Id_button_id);
button.setClickedListener(Componentt->{
/*添加Button 點擊后的處理action*/
Intent intent1 = new Intent();
Operation operation = new Intent.OperationBuilder()
.withDeviceId("")/*設置目標設備的ID ,"" 為本機*/
.withBundleName("com.example.firstapp")/*指定跳轉目標所在的應用包名*/
.withAbilityName("com.example.firstapp.SecondAbility")/*目標所在的類名*/
.build();
intent1.setOperation(operation);
/*調用一個方法,跳轉到SecondAbility adbility*/
startAbility(intent1);
});
2.Code頁面Button
button.setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
Intent intent1 = new Intent();
Operation operation = new Intent.OperationBuilder()
.withDeviceId("")
.withBundleName("com.example.firstapp")
.withAbilityName("com.example.firstapp.MainAbility")
.build();
intent1.setOperation(operation);
startAbility(intent1);
}
});
5.java文件說明
MyApplication 為項目應用默認入口類
MainAbility 為應用默認加載的Ability類MyApplicationMainAbility為 應用默認加載的Ability類MyApplication中調用
SecondAbility 為添加第二頁面增加的ability
MainAbilitySlice 為第一個XML布局加載和顯示界面的類
SecondAbilitySlice 為第二頁面實現類
作者:czdIT
想了解更多內容,請訪問: 51CTO和華為官方戰略合作共建的鴻蒙技術社區https://harmonyos.51cto.com