首页
找靠谱产品
找解决方案
找靠谱公司
找案例
找对的人
专家智库
悬赏任务
SAAS
ToB门户
了解全球最新的ToB事件
论坛
潜水/灌水快乐,沉淀知识,认识更多同行。
ToB圈子
加入IT圈,遇到更多同好之人。
微博
Follow
记录
Doing
博客
Blog
文库
业界最专业的IT文库,上传资料也可以赚钱
下载
分享
Share
排行榜
Ranklist
相册
Album
应用中心
qidao123.com ToB IT社区-企服评测·应用市场
»
论坛
›
软件与程序人生
›
移动端开发
›
鸿蒙
›
HarmonyOS(鸿蒙)——Text(文本)组件先容 ...
返回列表
发新帖
HarmonyOS(鸿蒙)——Text(文本)组件先容
[复制链接]
发表于 2026-2-10 14:13:00
|
显示全部楼层
|
阅读模式
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要
登录
才可以下载或查看,没有账号?
立即注册
×
| text_size | 文本巨细 | float范例 | 体现字体巨细的float范例。可以是浮点数值,其默认单位为px;也可以是带px/vp/fp单位的浮点数值;也可以引用float资源。 | ohos:text_size=“30"ohos:text_size=“16fp"ohos:text_size=”$float:size_value” |
代码
演示:
<Text
ohos:id=“$+id:text”
ohos:height=“match_content”
ohos:width=“match_content”
ohos:text=“150px”
ohos:text_size=“150px”/>
体现结果:
2.3 text_color
|
属性名称
|
中文形貌
|
取值
|
取值分析
|
利用案例
|
| — | — | — | — | — |
| text_color | 文本颜色 | color范例 | 可以直接设置色值,也可以引用color资源。 | ohos:text_color=“#A8FFFFFF"ohos:text_color=”$color:black" |
代码
演示:
<Text
ohos:id=“$+id:text”
ohos:height=“match_content”
ohos:width=“match_content”
ohos:text=“blue”
ohos:text_size=“100px”
ohos:text_color=“blue”
/>
演示结果:
利用HEX结果:
2.4 text_font
|
属性名称
|
中文形貌
|
取值
|
取值分析
|
利用案例
|
| — | — | — | — | — |
| text_font | 字体 | sans-serif | 可以设置的字体如表中所列。 | ohos:text_font=“HwChinese-medium” |
| | | sans-serif-medium | | |
| | | HwChinese-medium | | |
| | | sans-serif-condensed | | |
| | | sans-serif-condensed-medium | | |
| | | monospace | | |
代码
演示:
<Text
ohos:id=“$+id:text”
ohos:height=“match_content”
ohos:width=“match_content”
ohos:text=“李子捌”
ohos:text_size=“100px”
ohos:text_color=“#000000”
ohos:text_font=“HwChinese-medium”
/>
演示结果:
2.5 italic
|
属性名称
|
中文形貌
|
取值
|
取值分析
|
利用案例
|
| — | — | — | — | — |
| italic | 文本是否斜体字体 | boolean范例 | 可以直接设置true/false,也可以引用boolean资源。 | ohos:italic=“true"ohos:italic=”$boolean:true" |
代码演示:
<Text
ohos:id=“$+id:text”
ohos:height=“match_content”
ohos:width=“match_content”
ohos:text=“李子捌”
ohos:text_size=“100px”
ohos:text_color=“#000000”
ohos:text_font=“sans-serif”
ohos:italic=“true”
/>
演示结果:
2.6 text_weight
|
属性名称
|
中文形貌
|
取值
|
取值分析
|
利用案例
|
| — | — | — | — | — |
| text_weight | 字重 | integer范例 | 体现字体巨细的integer范例。也可以引用integer资源。 | ohos:text_weight=“100"ohos:text_weight=”$integer:100" |
代码演示:
<Text
ohos:id=“$+id:text”
ohos:height=“match_content”
ohos:width=“match_content”
ohos:text=“Liziba”
ohos:text_size=“100px”
ohos:text_color=“#0000FF”
ohos:text_font=“serif”
ohos:italic=“true”
ohos:text_weight=“100”
/>
演示结果:
text_weight = 100
text_weight = 600
2.7 background_element
常用的配景如常见的文本配景、按钮配景,可以采取XML格式放置在graphic目次下。
在Project窗口,打开“entry > src > main > resources > base”,右键点击“graphic”文件夹,选择“New > File”,定名为“background_text.xml”,在background_text.xml中界说文本的配景。
代码演示:
<?xml version="1.0" encoding="UTF-8" ?> <shape xmlns
hos=“http://schemas.huawei.com/res/ohos”
ohos:shape=“rectangle”>
演示结果:
2.8 text_alignment
|
属性名称
|
中文形貌
|
取值
|
取值分析
|
利用案例
|
| — | — | — | — | — |
| text_alignment | 文本对齐方式 | left | 体现文本靠左对齐。 | 可以设置取值项如表中所列,也可以利用“ |
| | | top | 体现文本靠顶部对齐。 | |
| | | right | 体现文本靠右对齐。 | |
| | | bottom | 体现文本靠底部对齐。 | |
| | | horizontal_center | 体现文本程度居中对齐。 | |
| | | vertical_center | 体现文本垂直居中对齐。 | |
| | | center | 体现文本居中对齐。 | |
| | | start | 体现文本靠起始端对齐。 | |
| | | end | 体现文本靠末了端对齐。 | |
代码演示:
<Text
ohos:id=“$+id:text”
ohos:width=“300vp”
ohos:height=“100vp”
ohos:text=“Liziba”
ohos:text_size=“100px”
ohos:text_color=“#0000FF”
ohos:text_font=“serif”
ohos:italic=“true”
ohos:text_weight=“100”
ohos:left_margin=“15vp”
ohos:bottom_margin=“15vp”
ohos:right_padding=“15vp”
ohos:left_padding=“15vp”
ohos:text_alignment=“bottom|horizontal_center”
ohos:background_element=“$graphic:background_text”
/>
演示结果:
2.9 multiple_lines
|
属性名称
|
中文形貌
|
取值
|
取值分析
|
利用案例
|
| — | — | — | — | — |
| multiple_lines | 多行模式设置 | boolean范例 | 可以直接设置true/false,也可以引用boolean资源。 | ohos:multiple_lines=“true"ohos:multiple_lines=”$boolean:true" |
代码演示:
<Text
ohos:id=“$+id:text”
ohos:width=“300px”
ohos:height=“match_content”
ohos:text=“LizibaLiziba”
ohos:text_size=“100px”
ohos:text_color=“#0000FF”
ohos:text_font=“serif”
ohos:italic=“true”
ohos:text_weight=“100”
ohos:max_text_lines=“2”
ohos:multiple_lines=“true”
ohos:background_element=“$graphic:background_text”
/>
演示结果:
2.10 max_text_lines
|
属性名称
|
中文形貌
|
取值
|
取值分析
|
利用案例
|
| — | — | — | — | — |
| max_text_lines | 文本最大行数 | integer范例 | 可以直接设置整型数值,也可以引用integer资源。 | ohos:max_text_lines=“2"ohos:max_text_lines=”$integer:two" |
代码演示:
<Text
ohos:id=“$+id:text”
ohos:width=“300px”
ohos:height=“match_content”
ohos:text=“LizibaLizibaLiziba”
ohos:text_size=“100px”
ohos:text_color=“#0000FF”
ohos:text_font=“serif”
ohos:italic=“true”
ohos:text_weight=“100”
ohos:multiple_lines=“true”
ohos:max_text_lines=“2”
ohos:background_element=“$graphic:background_text”
/>
演示结果:
2.11 auto_font_size
|
属性名称
|
中文形貌
|
取值
|
取值分析
|
利用案例
|
| — | — | — | — | — |
| auto_font_size | 是否支持文本主动调解文本字体巨细 | boolean范例 | 可以直接设置true/false,也可以引用boolean资源。 | ohos:auto_font_size=“true"ohos:auto_font_size=”$boolean:true" |
代码演示:
方法一:
<Text
ohos:id=“$+id:text”
ohos:width=“300px”
ohos:height=“match_content”
ohos:text=“LizibaLizibaLiziba”
ohos:text_size=“100px”
ohos:text_color=“#0000FF”
ohos:text_font=“serif”
ohos:italic=“true”
ohos:text_weight=“100”
ohos:max_text_lines=“1”
ohos:auto_font_size=“true”
ohos:background_element=“$graphic:background_text”
/>
演示结果:
方法二:
回复
使用道具
举报
返回列表
浏览过的版块
Java
物联网
Oracle
虚拟化与私有云
图数据库
SAAS
软件测试
IOS
人工智能
DevOps与敏捷开发
光之使者
+ 我要发帖
登录后关闭弹窗
登录参与点评抽奖 加入IT实名职场社区
去登录
微信订阅号
微信服务号
微信客服(加群)
H5
小程序
快速回复
返回顶部
返回列表