HarmonyOS 鸿蒙Next中如何修改SaveButton按压状态样式?例如修改按压状态的背景色。

HarmonyOS 鸿蒙Next中如何修改SaveButton按压状态样式?例如修改按压状态的背景色。 如何修改SaveButton按压状态样式?例如修改按压状态的背景色。

3 回复

saveButton不支持通用属性,支持安全控件的通用属性:安全控件通用属性-安全-ArkTS组件-ArkUI(方舟UI框架)-应用框架 - 华为HarmonyOS开发者

如果你想对文件进行保存到手机,也可以通过授权弹窗的方式来进行保存(photoHelper.showAssetsCreationDialog)

更多关于HarmonyOS 鸿蒙Next中如何修改SaveButton按压状态样式?例如修改按压状态的背景色。的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在鸿蒙Next中修改SaveButton按压状态样式,需在XML布局文件或自定义组件中定义Pressed状态背景色。使用ohos:background_element属性指定状态列表资源,示例代码:

<SaveButton
    ohos:background_element="$graphic:save_button_state"/>

resources/base/graphic/目录创建save_button_state.xml,内容为:

<state-container
    xmlns:ohos="http://schemas.huawei.com/res/ohos">
    <item state="pressed" element="$color:your_pressed_color"/>
</state-container>

your_pressed_color需在resources/base/color/中预先定义。

在HarmonyOS Next中修改SaveButton按压状态样式,可以通过自定义Button样式实现。以下是具体方法:

  1. 在resources/base/element目录下创建button.json样式文件

  2. 定义按压状态样式:

{
  "button": {
    "save-button": {
      "background-color": "#007DFF",
      "states": {
        "pressed": {
          "background-color": "#0055CC"
        }
      }
    }
  }
}
  1. 在布局文件中应用样式:
<Button
  ohos:id="$+id:saveButton"
  ohos:width="match_content"
  ohos:height="match_content"
  ohos:text="Save"
  ohos:background_element="$graphic:save_button"
  ohos:clickable="true"/>
  1. 或者在代码中动态设置:
Button saveButton = (Button) findComponentById(ResourceTable.Id_saveButton);
ShapeElement normalBg = new ShapeElement();
normalBg.setRgbColor(new RgbColor(0, 125, 255));

ShapeElement pressedBg = new ShapeElement();
pressedBg.setRgbColor(new RgbColor(0, 85, 204));

StateElement stateElement = new StateElement();
stateElement.addState(new int[]{ComponentState.PRESSED}, pressedBg);
stateElement.addState(new int[]{ComponentState.EMPTY}, normalBg);

saveButton.setBackground(stateElement);

注意按压状态的颜色建议比正常状态深一些,这样能提供更好的视觉反馈效果。

回到顶部