开发者在使用Button组件时,对组件的背景色设置有以下几个经典问题:
@Entry
@Component
struct ButtonSolution1 {
build() {
Column() {
Button('test')
.width('100%')
.height(40)
.margin({ top: 50 })
.borderRadius(10)
.backgroundColor('#00000000')
// 设置渐变色
.linearGradient({
angle: 90,
colors: [['#8E2233', 0.0], ['#D4344C', 0.3], ['#F48899', 0.7], ['#FBD7DD', 1]]
});
}
.height('100%')
.width('100%')
.padding({ left: 16, right: 16 })
.alignItems(HorizontalAlign.Center);
}
} 效果预览:

@Entry
@Component
struct ButtonSolution2 {
@State isEnable: boolean = true;
@Styles
disabledStyles(): void {
.backgroundColor('#F7B0BB')
.borderRadius(10)
.borderStyle(BorderStyle.Solid)
.borderWidth(2)
.borderColor('#FBD7DD')
.width('100%')
.height(40)
.opacity(1);
}
@Styles
normalStyles(): void {
.backgroundColor('#0A59F7')
.borderRadius(10)
.borderStyle(BorderStyle.Solid)
.borderWidth(2)
.borderColor('#FBD7DD')
.width('100%')
.height(40)
.opacity(1);
}
build() {
Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center }) {
Text('normal')
.fontSize(14)
.fontColor(Color.White)
.opacity(0.5)
.stateStyles({
normal: this.normalStyles,
})
.margin({ bottom: 20, top: 50 })
.textAlign(TextAlign.Center);
Button(this.isEnable === true ? 'enabled:true' : 'enabled:false')
.enabled(this.isEnable)
.borderRadius(10)
.borderStyle(BorderStyle.Solid)
.borderWidth(2)
.borderColor('#FBD7DD')
.width('100%')
.height(40)
.opacity(1)
.fontSize(14)
.fontColor(Color.White)
.margin({ bottom: 20 });
Button(this.isEnable === true ? 'effective' : 'disabled')
.backgroundColor('#0A59F7')
.borderRadius(10)
.borderStyle(BorderStyle.Solid)
.borderWidth(2)
.borderColor('#FBD7DD')
.width('100%')
.height(40)
.opacity(1)
.fontSize(14)
.fontColor(Color.White)
.enabled(this.isEnable)
.stateStyles({
disabled: this.disabledStyles,
})
.margin({ bottom: 20 });
Text(this.isEnable === true ? 'effective' : 'disabled')
.backgroundColor('#0A59F7')
.borderRadius(10)
.borderStyle(BorderStyle.Solid)
.borderWidth(2)
.borderColor(Color.Gray)
.width('100%')
.height(40)
.opacity(1)
.fontSize(14)
.fontColor(Color.White)
.enabled(this.isEnable)
.stateStyles({
disabled: this.disabledStyles,
})
.margin({ bottom: 20 })
.textAlign(TextAlign.Center);
Text('control disabled')
.margin({ bottom: 20 })
.onClick(() => {
this.isEnable = !this.isEnable;
console.info(`${this.isEnable}`);
});
}
.height('100%')
.padding(16);
}
} 效果预览:

@Entry
@Component
struct ButtonSolution3 {
build() {
Column() {
Button('Button-默认背景色', { type: ButtonType.Normal })
.width('100%')
.height(40)
.fontSize(16)
.fontColor(Color.Black)
.borderRadius(10)
.fontWeight(500)
.margin({ top: 50 });
Button('Button-默认背景下设置渐变色', { type: ButtonType.Normal })
.width('100%')
.height(40)
.fontSize(16)
.fontColor(Color.Black)
.borderRadius(10)
.fontWeight(500)
.linearGradient({
direction: GradientDirection.Left,
colors: [['rgba(255, 117, 218, 0.5)', 0.1],
['rgba(255, 255, 255, 0.8)', 1.0]]
})
.margin({ top: 20 });
Button('Button-透明背景色下设置渐变色', { type: ButtonType.Normal })
.width('100%')
.height(40)
.fontSize(16)
.fontColor(Color.Black)
.borderRadius(10)
.fontWeight(500)
.backgroundColor('#00000000')
.linearGradient({
direction: GradientDirection.Left,
colors: [['rgba(255, 117, 218, 0.5)', 0.1],
['rgba(255, 255, 255, 0.8)', 1.0]]
})
.margin({ top: 20 });
}
.width('100%')
.height('100%')
.padding({ left: 20, right: 20 });
}
} 效果预览:

@Entry
@Component
struct ButtonSolution4 {
build() {
Flex({ justifyContent: FlexAlign.SpaceAround }) {
// 点击时无响应
Button('disable')
.enabled(false)
.backgroundColor(0x317aff);
// 相同样式效果
Button('enable')
.backgroundColor(0x317aff)
.opacity(0.4);
}
.width('100%')
.margin({ top: 50 });
}
} 效果预览:

@Entry
@Component
struct BgColorSwitchPage {
// 自定义调色板颜色
private bgColorList: string[] = [
'#ff89fafa', '#ff8a8af6', '#ffcd89fa', '#fff689f6',
'#fffa7d7d', '#fff5c885', '#fff6f688', '#ff8bf88b',
'#ff4e4d4d', '#666666', '#999999', '#CCCCCC'
];
// 当前选中的颜色索引(响应式状态)
@State currentColorIndex: number = 0;
build() {
Column() {
Button('点击切换颜色')
.width(250)
.height(80)
.fontSize(18)
.fontWeight(FontWeight.Medium)
.fontColor('#FFFFFF')
.fontColor('#ff050505')
.backgroundColor(this.bgColorList[this.currentColorIndex]) // 设置按钮背景颜色
.borderRadius(16)
// 点击按钮组件切换颜色
.onClick(() => {
// 循环切换颜色索引
this.currentColorIndex = (this.currentColorIndex + 1) % this.bgColorList.length;
});
// 调色板标题
Text('自定义调色板')
.fontSize(16)
.margin({ top: 50, bottom: 15 });
// 调色板布局:Flex自动换行
Flex({ wrap: FlexWrap.Wrap, justifyContent: FlexAlign.Center }) {
ForEach(
this.bgColorList,
(color: string, index: number) => {
Column()
.width(40)
.height(40)
.backgroundColor(color)
.borderRadius(8)
.margin(8)
// 点击色块切换到对应颜色
.onClick(() => {
this.currentColorIndex = index;
})
// 当前选中的色块添加黑色边框高亮
.border({
width: this.currentColorIndex === index ? 2 : 0,
color: '#000000',
style: BorderStyle.Solid
});
},
(index: number) => index.toString()
);
}
.width('100%');
}
.width('100%')
.height('100%')
.justifyContent(FlexAlign.Center)
.padding(20);
}
} 效果预览:

@Entry
@Component
struct BgStyles {
@State message: string = '文本内容';
@State index: number = 0;
aboutToAppear(): void {
this.message = '文本内容' + this.index;
}
build() {
Column() {
Button() {
Text(this.message).fontSize(40);
}
.customStateStyle() // 调用自定义样式扩展
.onClick(() => {
this.index++;
if (this.index >= 10) {
this.index = 0;
}
this.message = '文本内容' + this.index;
});
}
.justifyContent(FlexAlign.Center)
.height('100%')
.width('100%');
}
}
// 自定义方法
@Extend(Button)
function customStateStyle() {
// 设置组件不同状态下的样式
.stateStyles({
clicked: {
.backgroundColor(Color.Transparent);
},
focused: {
.backgroundColor(Color.Transparent);
},
normal: {
.backgroundColor(Color.Transparent);
},
pressed: {
.backgroundColor(Color.Transparent);
},
selected: {
.backgroundColor(Color.Transparent);
},
disabled: {
.backgroundColor(Color.Transparent);
},
})
// 用于控制是否开启状态动效
.stateEffect(false);
} 效果预览:

Q:Button设置渐变背景色为什么未生效?
A:设置颜色渐变需先设置backgroundColor为透明色,详见ButtonType枚举说明。
Q:Button组件的默认背景色是什么?
A:Button组件的默认背景色为“#0A59F7”。