zoukankan      html  css  js  c++  java
  • HarmonyOS实战—实现跑马灯效果

    1. Text文本框展示大段内容文字

    • 文本中展示大段文字,除了这种方式之外,还有其他方式
      在这里插入图片描述

    • 可以使用跑马灯的形式展示,但需要两个前提条件,如下:
      下面两个都是默认属性,也可以省略不写
      在这里插入图片描述

    • ohos:truncation_mode="ellipsis_at_start",表示前面的内容省略掉,以“...”的形式,如:

    <Text
    	ohos:id="$+id:text1"
    	ohos:height="100vp"
    	ohos:width="100vp"
    	ohos:background_element="#55121212"
    	ohos:text="小明:你说我这穷日子过到啥时侯是个头啊?小红:那得看你能活多久了"
    	ohos:text_size="40vp"
    	ohos:truncation_mode="ellipsis_at_start"
       />
    

    在这里插入图片描述

    • 把宽度改为 300vp
      在这里插入图片描述
    • 如果想显示前面的内容,省略后面的内容,只要把 ohos:truncation_mode="ellipsis_at_end"
      在这里插入图片描述
    • ohos:truncation_mode="auto_scrolling"表示滚动效果
    • ohos:auto_scrolling_count="10"表示跑马灯滚动的次数,10表示滚动十次,unlimited表示无限次数
    • ohos:auto_scrolling_duration="2000"表示跑的速度,2000是时间单位,毫秒,多少时间跑完,表示2秒跑完这段内容

    2. 实现案例

    • 新建项目:TextLargeApplication

    ability_main

    <?xml version="1.0" encoding="utf-8"?>
    <DirectionalLayout
        xmlns:ohos="http://schemas.huawei.com/res/ohos"
        ohos:height="match_parent"
        ohos:width="match_parent"
        ohos:alignment="center"
        ohos:orientation="vertical">
    
        <Text
            ohos:id="$+id:text1"
            ohos:height="100vp"
            ohos:width="300vp"
            ohos:background_element="#55121212"
            ohos:text="小明:你说我这穷日子过到啥时侯是个头啊?小红:那得看你能活多久了"
            ohos:text_size="40vp"
            ohos:truncation_mode="auto_scrolling"
            ohos:auto_scrolling_count="unlimited"
            ohos:auto_scrolling_duration="2000"
            />
    
    </DirectionalLayout>
    

    MainAbilitySlice

    package com.xdr630.textlargeapplication.slice;
    
    import com.xdr630.textlargeapplication.ResourceTable;
    import ohos.aafwk.ability.AbilitySlice;
    import ohos.aafwk.content.Intent;
    import ohos.agp.components.Component;
    import ohos.agp.components.Text;
    
    public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener {
        @Override
        public void onStart(Intent intent) {
            super.onStart(intent);
            super.setUIContent(ResourceTable.Layout_ability_main);
    
            //1.获取Text
            Text text1 = (Text) findComponentById(ResourceTable.Id_text1);
    
            //2.给Text文本添加单击事件
            //表示当单击一下的时候,开启跑马灯效果
            text1.setClickedListener(this);
        }
    
        @Override
        public void onActive() {
            super.onActive();
        }
    
        @Override
        public void onForeground(Intent intent) {
            super.onForeground(intent);
        }
    
        @Override
        public void onClick(Component component) {
            //开启跑马灯效果
            //两种方式获取文本的对象
            //1.方法的参数,参数表示被点击组件的对象
            //2.可以把 onStart 方法中的Text对象,挪到成员位置
    
            //使用第一种方法实现:
            //先强转,因为开启跑马灯的方法不是父类component里的方法,而是Text文本里的方法
            //所以,把component强转为Text
            Text t = (Text) component;
            t.startAutoScrolling();
        }
    }
    
    • 运行:
      在这里插入图片描述
    • 因为设置了auto_scrolling_count="unlimited属性,所以会无限次的滚动。当然也可以设置滚动多少次,以及滚动的时间。
      在这里插入图片描述
    • 【本文正在参与“有奖征文 | HarmonyOS征文大赛”活动】
      https://marketing.csdn.net/p/ad3879b53f4b8b31db27382b5fc65bbc

    本文来自博客园,作者:兮动人,转载请注明原文链接:https://www.cnblogs.com/xdr630/p/15254550.html

  • 相关阅读:
    react-navigation
    react
    generator-react-webpack
    安装Eclipse反编译插件(jadclipse)
    Python爬虫学习笔记3:基本库的使用
    Python爬虫学习笔记2:爬虫基础
    Python爬虫学习笔记1:request、selenium、ChromeDrive、GeckoDriver等相关依赖安装
    Python学习笔记28:面向对象进阶及 hashlib
    Python学习笔记27:反射,类的内置方法
    Python学习笔记26:封装、@property、@staticmethod和@classmethod装饰器方法、反射getattr()
  • 原文地址:https://www.cnblogs.com/xdr630/p/15254550.html
Copyright © 2011-2022 走看看