> 文档中心 > HarmonyOS APP开发入门3——组件(二 Text组件)

HarmonyOS APP开发入门3——组件(二 Text组件)


HarmonyOS APP开发入门3——组件(二 Text组件)

文章目录

  • HarmonyOS APP开发入门3——组件(二 Text组件)
        • Text组件

Text组件


文本(Text)是用来显示字符串的组件,在界面上显示为一块文本区域。仅仅作为展示数据使用,用户不能在App中修改文本组件中的内容。

Text组件是最基本的组件,后面还会学习他的子类组件,比如Button,TextField都是从这个类衍生而来的。

常见的属性

这些属性不用去背,用着用着就熟了,想要对文本进行一个设置,如果忘记属性,可以直接到笔记中找,或者到华为开发者文档中找。

常用属性

功能说明 属性名称
id属性,唯一 id
组件宽 width
组件高 height
文本内容 text
背景,可以设颜色值或xml背景 background_element
文字颜色 text_color
文字粗细 text_weight
斜体 italic
文字尺寸单位: fp text_size
字体。如: 黑体、宋体、楷体 text_font
文字内容对齐方式 text_alignment
左/右外间距 left_margin / right_margin
上下外间距 top_magin / bottom_margin
左/右内间距 left_padding / right_padding
上下内间距 top_padding/ bottom_padding
文本内容换行 paddingmultiple_lines
最大文本显示行数 max_text_lines
自动调节文字大小 auto_font_size
常见属性 <Text ohos:id="$+id:text" ohos:width="match_content" ohos:height="match_content" ohos:text="Text" ohos:background_element="$graphic:color_gray_element" />其他属性使用方式: 字体大小 ohos:text_size="28fp" 字体颜色 ohos:text_color="blue" Text组件的左外边距 ohos:left_margin="15vp" Text组件的下外边距 ohos:bottom_margin="15vp" Text组件和内部文本的右内边距 ohos:right_padding="15vp" Text组件和内部文本的左内边距 ohos:left_padding="15vp" 设置字体风格 斜体ohos:italic="true" 字重(就是文字的粗细) ohos:text_weight="700" 字体ohos:text_font="serif" 设置文本对齐方式ohos:text_alignment="horizontal_center|bottom" 设置文本换行 ohos:multiple_lines="true" 最大显示行数 ohos:max_text_lines="2"

创建Text

在layout目录下的xml文件中创建Text组件。

<Text    ohos:id="$+id:text"    ohos:width="match_content"    ohos:height="match_content"    ohos:text="Text"/>

设置Text

在xml中设置Text的背景。

<Text    ...    ohos:background_element="$graphic:background_text"/>

设置字体大小和颜色

<Text    ohos:id="$+id:text"    ohos:width="match_content"    ohos:height="match_content"    ohos:text="Text"    ohos:text_size="28fp"    ohos:text_color="#0000FF"    ohos:left_margin="15vp"    ohos:bottom_margin="15vp"    ohos:right_padding="15vp"    ohos:left_padding="15vp"    ohos:background_element="$graphic:background_text"/>

设置字体风格和字重

<Text    ohos:id="$+id:text"    ohos:width="match_content"    ohos:height="match_content"    ohos:text="Text"    ohos:text_size="28fp"    ohos:text_color="#0000FF"    ohos:italic="true"    ohos:text_weight="700"    ohos:text_font="serif"    ohos:left_margin="15vp"    ohos:bottom_margin="15vp"    ohos:right_padding="15vp"    ohos:left_padding="15vp"    ohos:background_element="$graphic:background_text"/>

设置文本对齐方式

<Text    ohos:id="$+id:text"    ohos:width="300vp"    ohos:height="100vp"    ohos:text="Text"    ohos:text_size="28fp"    ohos:text_color="#0000FF"    ohos:italic="true"    ohos:text_weight="700"    ohos:text_font="serif"    ohos:left_margin="15vp"    ohos:bottom_margin="15vp"    ohos:right_padding="15vp"    ohos:left_padding="15vp"    ohos:text_alignment="horizontal_center|bottom"    ohos:background_element="$graphic:background_text"/>

设置自动换行、最大显示行数和自动调节字体大小

<Text    ohos:id="$+id:text"    ohos:width="90vp"    ohos:height="match_content"    ohos:min_height="30vp"    ohos:text="T"    ohos:text_color="#0000FF"    ohos:italic="true"    ohos:text_weight="700"    ohos:text_font="serif"    ohos:multiple_lines="true"    ohos:max_text_lines="1"    ohos:auto_font_size="true"    ohos:right_padding="8vp"    ohos:left_padding="8vp"    ohos:background_element="$graphic:background_text"/>

跑马灯效果

<Text    ohos:id="$+id:text"    ohos:width="75vp"    ohos:height="match_content"    ohos:text="TextText"    ohos:text_size="28fp"    ohos:text_color="#0000FF"    ohos:italic="true"    ohos:text_weight="700"    ohos:text_font="serif"    ohos:background_element="$graphic:background_text"/>
// 跑马灯效果text.setTruncationMode(Text.TruncationMode.AUTO_SCROLLING);// 始终处于自动滚动状态text.setAutoScrollingCount(Text.AUTO_SCROLLING_FOREVER);// 启动跑马灯效果text.startAutoScrolling();

喜欢博文可以关注博主哦,以后会持续更新干货的