鸿蒙OS Text

2020-09-18 11:53 更新

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

使用 Text

  • 创建 Text

  1. <Text
  2. ohos:id="$+id:text"
  3. ohos:width="match_content"
  4. ohos:height="match_content"
  5. ohos:text="Text"
  6. ohos:background_element="$graphic:color_gray_element"/>

color_gray_element.xml:

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

图1 创建一个 Text img

  • 设置背景

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

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

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

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

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

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

  1. <Text
  2. ohos:id="$+id:text"
  3. ohos:width="match_content"
  4. ohos:height="match_content"
  5. ohos:text="Text"
  6. ohos:background_element="$graphic:textelement"/>

  • 设置字体大小和颜色

  1. <Text
  2. ohos:id="$+id:text"
  3. ohos:width="match_content"
  4. ohos:height="match_content"
  5. ohos:text="Text"
  6. ohos:text_size="28fp"
  7. ohos:text_color="blue"
  8. ohos:left_margin="15vp"
  9. ohos:bottom_margin="15vp"
  10. ohos:right_padding="15vp"
  11. ohos:left_padding="15vp"
  12. ohos:background_element="$graphic:textelement"/>

图3 设置字体大小和颜色的效果 img

  • 设置字体风格和字重

  1. <Text
  2. ohos:id="$+id:text"
  3. ohos:width="match_content"
  4. ohos:height="match_content"
  5. ohos:text="Text"
  6. ohos:text_size="28fp"
  7. ohos:text_color="blue"
  8. ohos:italic="true"
  9. ohos:text_weight="700"
  10. ohos:text_font="serif"
  11. ohos:left_margin="15vp"
  12. ohos:bottom_margin="15vp"
  13. ohos:right_padding="15vp"
  14. ohos:left_padding="15vp"
  15. ohos:background_element="$graphic:textelement"/>

图4 设置字体风格和字重的效果 img

  • 设置文本对齐方式

  1. <Text
  2. ohos:id="$+id:text"
  3. ohos:width="300vp"
  4. ohos:height="100vp"
  5. ohos:text="Text"
  6. ohos:text_size="28fp"
  7. ohos:text_color="blue"
  8. ohos:italic="true"
  9. ohos:text_weight="700"
  10. ohos:text_font="serif"
  11. ohos:left_margin="15vp"
  12. ohos:bottom_margin="15vp"
  13. ohos:right_padding="15vp"
  14. ohos:left_padding="15vp"
  15. ohos:text_alignment="horizontal_center|bottom"
  16. ohos:background_element="$graphic:textelement"/>

图5 设置文本对齐方式的效果 点击放大

  • 设置文本换行和最大显示行数

  1. <Text
  2. ohos:id="$+id:text"
  3. ohos:width="75vp"
  4. ohos:height="match_content"
  5. ohos:text="TextText"
  6. ohos:text_size="28fp"
  7. ohos:text_color="blue"
  8. ohos:italic="true"
  9. ohos:text_weight="700"
  10. ohos:text_font="serif"
  11. ohos:multiple_lines="true"
  12. ohos:max_text_lines="2"
  13. ohos:background_element="$graphic:textelement"/>

图6 设置文本换行和最大显示行数的效果 img

自动调节字体大小

Text对象支持根据文本长度自动调整文本的字体大小和换行。

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

  1. <Text
  2. ohos:id="$+id:text1"
  3. ohos:width="90vp"
  4. ohos:height="match_content"
  5. ohos:min_height="30vp"
  6. ohos:text="T"
  7. ohos:text_color="blue"
  8. ohos:italic="true"
  9. ohos:text_weight="700"
  10. ohos:text_font="serif"
  11. ohos:multiple_lines="true"
  12. ohos:max_text_lines="1"
  13. ohos:auto_font_size="true"
  14. ohos:right_padding="8vp"
  15. ohos:left_padding="8vp"
  16. ohos:background_element="$graphic:textelement"/>

  1. 通过 setAutoFontSizeRule 设置自动调整规则,三个入参分别是最小的字体大小、最大的字体大小、每次调整文本字体大小的步长。

  1. // 设置自动调整规则
  2. text.setAutoFontSizeRule(30, 100, 1);
  3. // 设置点击一次增多一个"T"
  4. text.setClickedListener(new Component.ClickedListener() {
  5. @Override
  6. public void onClick(Component Component) {
  7. text.setText(text.getText() + "T");
  8. }
  9. });

图7 自动调节字体大小 img

跑马灯效果

当文本过长时,可以设置跑马灯效果,实现文本滚动显示。前提是文本换行关闭且最大显示行数为1,默认情况下即可满足前提要求。

  1. <Text
  2. ohos:id="$+id:text"
  3. ohos:width="75vp"
  4. ohos:height="match_content"
  5. ohos:text="TextText"
  6. ohos:text_size="28fp"
  7. ohos:text_color="blue"
  8. ohos:italic="true"
  9. ohos:text_weight="700"
  10. ohos:text_font="serif"
  11. ohos:background_element="$graphic:textelement"/>
  12. // 跑马灯效果
  13. text.setTruncationMode(Text.TruncationMode.AUTO_SCROLLING);
  14. // 启动跑马灯效果
  15. text.startAutoScrolling();

图8 跑马灯效果 img

场景示例

利用文本组件实现一个标题栏和详细内容的界面。

图9 界面效果

img

源码示例:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <DependentLayout
  3. xmlns:ohos="http://schemas.huawei.com/res/ohos"
  4. ohos:width="match_parent"
  5. ohos:height="match_content"
  6. ohos:background_element="$graphic:color_light_gray_element">
  7. <Text
  8. ohos:id="$+id:text1"
  9. ohos:width="match_parent"
  10. ohos:height="match_content"
  11. ohos:text_size="25fp"
  12. ohos:top_margin="15vp"
  13. ohos:left_margin="15vp"
  14. ohos:right_margin="15vp"
  15. ohos:background_element="$graphic:textelement"
  16. ohos:text="Title"
  17. ohos:text_weight="1000"
  18. ohos:text_alignment="horizontal_center"/>
  19. <Text
  20. ohos:id="$+id:text3"
  21. ohos:width="match_parent"
  22. ohos:height="120vp"
  23. ohos:text_size="25fp"
  24. ohos:background_element="$graphic:textelement"
  25. ohos:text="Content"
  26. ohos:top_margin="15vp"
  27. ohos:left_margin="15vp"
  28. ohos:right_margin="15vp"
  29. ohos:bottom_margin="15vp"
  30. ohos:text_alignment="center"
  31. ohos:below="$id:text1"
  32. ohos:text_font="serif"/>
  33. <Button
  34. ohos:id="$+id:button1"
  35. ohos:width="75vp"
  36. ohos:height="match_content"
  37. ohos:text_size="15fp"
  38. ohos:background_element="$graphic:textelement"
  39. ohos:text="Previous"
  40. ohos:right_margin="15vp"
  41. ohos:bottom_margin="15vp"
  42. ohos:left_padding="5vp"
  43. ohos:right_padding="5vp"
  44. ohos:below="$id:text3"
  45. ohos:left_of="$id:button2"
  46. ohos:text_font="serif"/>
  47. <Button
  48. ohos:id="$+id:button2"
  49. ohos:width="75vp"
  50. ohos:height="match_content"
  51. ohos:text_size="15fp"
  52. ohos:background_element="$graphic:textelement"
  53. ohos:text="Next"
  54. ohos:right_margin="15vp"
  55. ohos:bottom_margin="15vp"
  56. ohos:left_padding="5vp"
  57. ohos:right_padding="5vp"
  58. ohos:align_parent_end="true"
  59. ohos:below="$id:text3"
  60. ohos:text_font="serif"/>
  61. </DependentLayout>

color_light_gray_element.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <shape xmlns:ohos="http://schemas.huawei.com/res/ohos"
  3. ohos:shape="rectangle">
  4. <solid
  5. ohos:color="#ffeeeeee"/>
  6. </shape>

textelement.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <shape xmlns:ohos="http://schemas.huawei.com/res/ohos"
  3. ohos:shape="rectangle">
  4. <corners
  5. ohos:radius="20"/>
  6. <solid
  7. ohos:color="#ff888888"/>
  8. </shape>
以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号