对于大段文字在屏幕中的显示问题,一般常使用的有4种方式,即:前面省略、后面省略、中间省略、全文滚动(走马灯效果)。
具体在使用时,需要设置一个属性,即 truncation_mode
1、跑马灯效果:
实现跑马灯效果事先需要让组件满足:
- (1)关闭自动换行
- (2)最大文本数
- (3)truncation_mode属性值设置为 auto_scrolling
- 默认情况下,如果不特意设置下面两个属性,就直接满足跑马灯的需求
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"
ohos:orientation="vertical">
<Text
ohos:id="$+id:text"
ohos:height="80vp"
ohos:width="match_parent"
ohos:text="大家好,我是[Star星屹程序设计]的分享人,希望大家持续关注我,相互学习,一起进步!!!"
ohos:text_size="40fp"
ohos:text_alignment="vertical_center"
ohos:background_element="cyan"
ohos:truncation_mode="auto_scrolling"
ohos:auto_scrolling_count="unlimited"
ohos:auto_scrolling_duration="2000"
/>
</DirectionalLayout>
Java中开启跑马灯效果:
package com.example.demo1.slice;
import com.example.demo1.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import java.util.ArrayList;
import java.util.Random;
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 = (Text) findComponentById(ResourceTable.Id_text);
//2、绑定点击事件
text.setClickedListener(this);
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
@Override
public void onClick(Component component) {
//开启跑马灯
((Text)component).startAutoScrolling();
}
}