一 概述
鸿蒙OS应用中,Text不能直接作为布局文件使用,需要结合布局文件一起使用(Android中可以)

public final void setUIContent(int layoutRes)
public void setUIContent(ComponentContainer componentContainer)

二、布局文件
ability_main.xml

<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:height="match_parent"
    ohos:width="match_parent"
   >

    <Text
        ohos:id="$+id:text"
        ohos:width="200vp"
        ohos:height="match_content"
        ohos:text="厦门大学欢迎您们!"
        ohos:text_size="28fp"
        ohos:text_color="#0000FF"
        ohos:italic="true"
        ohos:text_weight="700"
        ohos:text_font="serif"
        ohos:layout_alignment="horizontal_center"
        ohos:background_element="$graphic:background_ability_main"/>

三、代码设置
MainAbilitySlice

public class MainAbilitySlice extends AbilitySlice {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        Text text = (Text) findComponentById(ResourceTable.Id_text);
        // 跑马灯效果
        text.setTruncationMode(Text.TruncationMode.AUTO_SCROLLING);
        // 始终处于自动滚动状态
        text.setAutoScrollingCount(Text.AUTO_SCROLLING_FOREVER);
        // 启动跑马灯效果
        text.startAutoScrolling();
    }

四、效果图

在这里插入图片描述

Logo

讨论HarmonyOS开发技术,专注于API与组件、DevEco Studio、测试、元服务和应用上架分发等。

更多推荐