zl程序教程

您现在的位置是:首页 >  其他

当前栏目

HarmonyOS实战—实现跑马灯效果

2023-02-26 10:18:44 时间

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

  • 文本中展示大段文字,除了这种方式之外,还有其他方式

HarmonyOS实战—实现跑马灯效果

  • 可以使用跑马灯的形式展示,但需要两个前提条件,如下:

下面两个都是默认属性,也可以省略不写
HarmonyOS实战—实现跑马灯效果

(福利推荐:阿里云、腾讯云、华为云服务器最新限时优惠活动,云服务器1核2G仅88元/年、2核4G仅698元/3年,点击这里立即抢购>>>

  • 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"    />

HarmonyOS实战—实现跑马灯效果

  • 把宽度改为 300vp

HarmonyOS实战—实现跑马灯效果

  • 如果想显示前面的内容,省略后面的内容,只要把 ohos:truncation_mode="ellipsis_at_end"

HarmonyOS实战—实现跑马灯效果

  • 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();     } }
  • 运行:

HarmonyOS实战—实现跑马灯效果

  • 因为设置了auto_scrolling_count="unlimited属性,所以会无限次的滚动。当然也可以设置滚动多少次,以及滚动的时间。

HarmonyOS实战—实现跑马灯效果

HarmonyOS实战—实现跑马灯效果


本站部分内容转载自网络,版权属于原作者所有,如有异议请联系QQ153890879修改或删除,谢谢!
转载请注明原文链接:HarmonyOS实战—实现跑马灯效果

你还在原价购买阿里云、腾讯云、华为云、天翼云产品?那就亏大啦!现在申请成为四大品牌云厂商VIP用户,可以3折优惠价购买云服务器等云产品,并且可享四大云服务商产品终身VIP优惠价,还等什么?赶紧点击下面对应链接免费申请VIP客户吧:

1、点击这里立即申请成为腾讯云VIP客户

2、点击这里立即注册成为天翼云VIP客户

3、点击这里立即申请成为华为云VIP客户

4、点击这里立享阿里云产品终身VIP优惠价

喜欢 (0)
[[email protected]]
分享 (0)