Scroll(this.scroller) {
Row() {
Text(this.longText)
.maxLines(1)
.fontSize(18)
.textAlign(TextAlign.End) // 右对齐,保证文字靠右排列,当文本可滚动时该属性不生效
}
.padding(10)
.borderRadius(20)
.backgroundColor('#f1f3f5')
.justifyContent(FlexAlign.End) // 确保内容在行内右对齐
}
.height(100)
.width('100%')
.align(Alignment.End) // 容器内子元素尾端对齐
.scrollBar(BarState.Off)
.scrollable(ScrollDirection.Horizontal) 通过设置文本内容新增时,滚动容器中的内容自动滚动到容器边缘,实现文本始终右对齐的效果。
@Entry
@Component
struct TextExample1 {
private scroller: Scroller = new Scroller();
@State longText: string = 'TextAlign set to End.1111111111';
build() {
Column() {
Text('textAlign')
.fontSize(28)
.fontColor(0xCCCCCC);
Scroll(this.scroller) {
Row() {
Text(this.longText)
.maxLines(1)
.fontSize(18)
.textAlign(TextAlign.End); // 右对齐,保证文字靠右排列
}
.padding(10)
.borderRadius(20)
.backgroundColor('#f1f3f5')
.justifyContent(FlexAlign.End); // 确保内容在行内右对齐
}
.height(100)
.width('100%')
.align(Alignment.End)
.scrollBar(BarState.Off)
.scrollable(ScrollDirection.Horizontal);
Button('PLUS')
.height('5%')
.onClick(() => {
this.longText = this.longText + '2';
this.scroller.scrollEdge(Edge.End); // 强制滚动到最右边
});
}
.width('100%')
.padding(28);
}
}