三、代码示例
布局文件示例 :
<?xml version="1.0" encoding="utf-8"?> <DirectionalLayout xmlns:ohos="http://schemas.huawei.com/res/ohos" ohos:height="match_parent" ohos:width="match_parent" ohos:orientation="vertical"> <Text ohos:id="$+id:text_helloworld1" ohos:height="match_content" ohos:width="match_content" ohos:background_element="#000000" ohos:layout_alignment="horizontal_center" ohos:text="Hello World" ohos:text_size="100" ohos:text_color="#00FF00"/> <Text ohos:id="$+id:text_helloworld2" ohos:height="match_content" ohos:width="match_content" ohos:background_element="#00FF00" ohos:layout_alignment="horizontal_center" ohos:text="Hello World" ohos:text_size="100" ohos:text_color="#00FF00"/> </DirectionalLayout>
Java 代码示例 :
package com.example.text.slice; import com.example.text.ResourceTable; import ohos.aafwk.ability.AbilitySlice; import ohos.aafwk.content.Intent; import ohos.agp.components.Text; import ohos.agp.utils.Color; public class MainAbilitySlice extends AbilitySlice { @Override public void onStart(Intent intent) { super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_main); // 获取布局中的组件 Text text = (Text) findComponentById(ResourceTable.Id_text_helloworld2); // 使用代码设置文本 text.setText("Hello In Java"); // 使用代码设置文字大小 text.setTextSize(150); // 使用代码设置文字颜色 text.setTextColor(Color.RED); } @Override public void onActive() { super.onActive(); } @Override public void onForeground(Intent intent) { super.onForeground(intent); } }
执行结果 :
四、GitHub 地址
GitHub 地址 : https://github.com/han1202012/HarmonyHelloWorld