HarmonyOS开发50:美化组件:实际开发中美化组件的方式

情况一:
美工已经把图片给你做好。
这种情况最简单,我们只要在state-container里面指定状态并关联图片即可。

做法:
新建register_state.xml文件,代码如下

<?xml version="1.0" encoding="utf-8"?>
<state-container
    xmlns:ohos="http://schemas.huawei.com/res/ohos">

    <item ohos:state="component_state_pressed" ohos:element="$media:registerpress"/>
    <item ohos:state="component_state_empty" ohos:element="$media:register"/>
</state-container>

ability_main.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:alignment="center"
    ohos:orientation="vertical">

    <Button
        ohos:height="31.4vp"
        ohos:width="85.4vp"
        ohos:background_element="$graphic:register_state"
        />

</DirectionalLayout>

运行效果:
请添加图片描述
情况二:

美工没有把图片给你做好。
这种情况稍显复杂,你可以给美工提出需求。如果人家不鸟咱,咱就要自己写。

做法:
新建register_empty.xml和register_press.xml两个文件
register_empty.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:shape="rectangle">

    <corners
        ohos:radius="15vp"/>

    <solid
        ohos:color="#21A8FD"/>

</shape>

register_press.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:shape="rectangle">

    <corners
        ohos:radius="15vp"/>

    <solid
        ohos:color="#0177BE"/>

</shape>

同时修改register_state.xml:

<?xml version="1.0" encoding="utf-8"?>
<state-container
    xmlns:ohos="http://schemas.huawei.com/res/ohos">

    <item ohos:state="component_state_pressed" ohos:element="$graphic:register_press"/>
    <item ohos:state="component_state_empty" ohos:element="$graphic:register_empty"/>

</state-container>

运行效果:
请添加图片描述

以上我们完成了对按钮的美化效果
同样的,开关美化也类似

ability_main.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:alignment="center"
    ohos:orientation="vertical">

    <!--<Button
        ohos:height="31.4vp"
        ohos:width="85.4vp"
        ohos:background_element="$graphic:register_state"
        />-->

    <Switch
        ohos:height="50vp"
        ohos:width="100vp"
        ohos:thumb_element="$graphic:register_state"/>

</DirectionalLayout>

修改register_state.xml:

<?xml version="1.0" encoding="utf-8"?>
<state-container
    xmlns:ohos="http://schemas.huawei.com/res/ohos">

    <!--<item ohos:state="component_state_pressed" ohos:element="$graphic:register_press"/>-->
    <item ohos:state="component_state_checked" ohos:element="$graphic:register_press"/>
    <item ohos:state="component_state_empty" ohos:element="$graphic:register_empty"/>

</state-container>

运行效果:
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GeniusAng丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值