日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【鸿蒙 HarmonyOS】UI 组件 ( Text 组件 )

發布時間:2025/6/17 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【鸿蒙 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 组件 )的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。