【鸿蒙 HarmonyOS】UI 组件 ( Text 组件 )
文章目錄
- 一、Text 組件
- 二、Module 準備
- 三、代碼示例
- 四、GitHub 地址
一、Text 組件
Text 組件是在 UI 界面中顯示文本的組件 ;
1. 布局文件中設置 Text :
Text 組件在布局文件中的示例 :
<?xml version="1.0" encoding="utf-8"?> <DirectionalLayoutxmlns:ohos="http://schemas.huawei.com/res/ohos"ohos:height="match_parent"ohos:width="match_parent"ohos:orientation="vertical"><Textohos:id="$+id:text_helloworld"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"/></DirectionalLayout>id 屬性 : ohos:id="$+id:text_helloworld" , 用于作為當前組件的唯一標識 , 在單個布局文件中不允許 id 標識重復 ;
寬度與高度屬性 : 可以設置 match_content 和 match_parent 兩個值 ;
- 寬度 : ohos:width=“match_content”
- 高度 : ohos:height=“match_content”
組件位置屬性 : ohos:layout_alignment=“horizontal_center” , 上述配置標識組件水平居中 ;
背景設置屬性 : ohos:background_element="$graphic:background_ability_main" , 可以設置一個顏色值 ;
文本設置 : ohos:text=“Hello World” , 設置組件顯示的文本為 Hello World ;
文本文字大小設置 : ohos:text_size=“50”
文本顏色設置 : ohos:text_color="#FF0000" , 紅色 ;
2. 代碼中設置 Text :
// 獲取布局中的組件Text text = (Text) findComponentById(ResourceTable.Id_text_helloworld2);// 使用代碼設置文本text.setText("Hello In Java");// 使用代碼設置文字大小text.setTextSize(150);// 使用代碼設置文字顏色text.setTextColor(Color.RED);獲取組件 : 調用 findComponentById ( ) 方法獲取 ;
設置文本 : 調用 Text 對象的 setText ( ) 方法設置文本 ;
設置文字大小 : 調用 Text 對象的 setTextSize ( ) 方法設置文字大小 ;
設置文字顏色 : 調用 Text 對象的 setTextColor ( ) 方法設置文字顏色 ;
二、Module 準備
繼續使用上一篇博客 【鴻蒙 HarmonyOS】界面跳轉 ( Page Ability 的 action 標識 | Page Ability 之間的界面跳轉及傳遞數據 | 鴻蒙工程下創建 Module | 代碼示例 ) 的項目進行演示 ;
在歡迎界面選擇左側 Version Control 中的 Git 選項 , 登錄 GitHub 賬號 , 將項目拉取到本地 ;
從 GitHub 上 Clone 代碼 :
參考之前的 【鴻蒙 HarmonyOS】界面跳轉 ( Page Ability 的 action 標識 | Page Ability 之間的界面跳轉及傳遞數據 | 鴻蒙工程下創建 Module | 代碼示例 ) 博客 , 創建 Module ;
三、代碼示例
布局文件示例 :
<?xml version="1.0" encoding="utf-8"?> <DirectionalLayoutxmlns:ohos="http://schemas.huawei.com/res/ohos"ohos:height="match_parent"ohos:width="match_parent"ohos:orientation="vertical"><Textohos: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"/><Textohos: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 {@Overridepublic 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);}@Overridepublic void onActive() {super.onActive();}@Overridepublic void onForeground(Intent intent) {super.onForeground(intent);} }執行結果 :
四、GitHub 地址
GitHub 地址 : https://github.com/han1202012/HarmonyHelloWorld
總結
以上是生活随笔為你收集整理的【鸿蒙 HarmonyOS】UI 组件 ( Text 组件 )的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【计算理论】计算理论总结 ( 图灵机设计
- 下一篇: 【Android 安全】DEX 加密 (