HarmongOS开发之Button控件讲解

Posted 码上夏雨

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HarmongOS开发之Button控件讲解相关的知识,希望对你有一定的参考价值。

为Button添加Background(针对XML对象)

XML内部使用

<Button
	ohos:id="$+id:bottom_play_btn"
    ohos:height="50vp"
    ohos:width="50vp"
    ohos:margin="10vp"
    ohos:background_element="$graphic:ic_play"/>

代码使用

首先我们看一下官方给定的方法:
setBackground方法
在这里给定的参数是Element类,查看文档可知下面的类有
ohos.agp.components.element.Element
其中关于 VectorElement的描述为
Provides an Element object that supports vector drawing.
You can use this class to draw vector elements to prevent image distortion after scaling.

VectorElement Detail
由此我们可以使用下面方法来将 xml 用来更新Background

playBtn.setBackground(new VectorElement(this,ResourceTable.Graphic_ic_pause));

以上是关于HarmongOS开发之Button控件讲解的主要内容,如果未能解决你的问题,请参考以下文章