更新时间:2022-06-25 16:14:49浏览次数:557+次
以下介绍鸿蒙HarmonyOS ETS如何给组件设置边框的方法,即如何给Text组件设置左上右上为圆角,现在的boder方法只能一起设置4个角,以下将给出解决方案。
解答
我们可以使用boder设置圆角,或者在text组件添加一个矩形的遮罩层。建议先阅读鸿蒙官方的边框设置、形状裁剪文档内容,有了这两个基础再来看以下的代码。
代码运行如下:
import featureAbility from '@ohos.ability.featureAbility';
@Entry
@Component
struct Index {
build() {
Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
Text("border").width("100%").height(100).fontSize(50).textAlign(TextAlign.Center)
Text("Z").width(100).height(100)
.border({color:Color.Blue,radius:10})//todo 设置边框
.fontSize(50)
.textAlign(TextAlign.Center)
.backgroundColor(Color.Red)
Text("设置两边圆角")
.width("100%")
.height(100)
.fontSize(50)
.textAlign(TextAlign.Center)
Text('Z')
.width(100)
.height(100)
.textAlign(TextAlign.Center)
.fontSize(50)
.backgroundColor(Color.Red)
//todo 从Text组件左上角开始
// todo 当Rect的width和height和Text组件的width和height大小相等时候 刚好覆盖text组件
.mask(new Rect({width: 100, height: 150}).fill(Color.White).radius(20))
}
.width('100%')
.height('100%')
}
}
效果截图如下:
相关资讯