了解鸿蒙OS Text组件

本文详述了鸿蒙OS中的Text组件用法,包括创建Text、设置背景、字体大小颜色、风格字重、对齐方式、自动调节字体大小及跑马灯效果。并提供场景示例展示如何构建标题栏和内容界面。
摘要由CSDN通过智能技术生成

文本(Text)是用来显示字符串的组件,在界面上显示为一块文本区域。Text 作为一个基本组件,有很多扩展,常见的有按钮组件 Button,文本编辑组件 TextField。

使用 Text

  • 创建 Text
 <Text
      ohos:id="$+id:text"
      ohos:width="match_content"
      ohos:height="match_content"
      ohos:text="Text"
      ohos:background_element="$graphic:color_gray_element"/>

color_gray_element.xml:

<?xml version="1.0" encoding="utf-8"?>
  <shape xmlns:ohos="http://schemas.huawei.com/res/ohos"
      ohos:shape="rectangle">
      <solid
          ohos:color="#ff888888"/>
  </shape>

图1 创建一个 Text

  • 设置背景

常用的背景如常见的文本背景、按钮背景,可以采用XML格式放置在 graphic 目录下。

在“Project”窗口,打开“entry > src > main > resources > base”,右键点击“base”文件夹,选择“New > Directory”,命名为“graphic”。右键点击“graphic”文件夹,选择“New > File”,命名为“textelement.xml”。

图2 创建 textelement.xml 文件后的 resources 目录结构

在 textelement.xml 中定义文本的背景:

<?xml version="1.0" encoding="utf-8"?>
  <shape xmlns:ohos="http://schemas.huawei.com/res/ohos"
      ohos:shape="rectangle">
      <corners
          ohos:radius="20"/>
      <solid
          ohos:color="#ff888888"/>
  </shape>

在 first_layout.xml 中引用上面定义的文本背景:

 <Text
      ohos:id="$+id:text"
      ohos:width="match_content"
      ohos:height="match_content"
      ohos:text="Text"
      ohos:background_element="$graphic:textelement"/>
  • 设置字体大小和颜色
 <Text
      ohos:id="$+id:tex
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值