harmonyOS编写一个颜色渐变的背景资源文件,引用不起作用

在graphic 文件下编写了一个 渐变的资源文件,在layout 中引用不起作用,资源文件代码如下:

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

    <corners
        ohos:radius="100"/>

    <gradient
        ohos:angle="0"
        ohos:center_color="#68D33C"
        ohos:end_color="#37C231"
        ohos:start_color="#91E344"
        ohos:type="linear"
        ohos:use_level="false"/>

</shape>

引用代码:
<Text
    ohos:height="50vp"
    ohos:width="200vp"
    ohos:background_element="$graphic:bt_green"/>

harmonyOS 背景颜
2021-07-05 16:25:15
浏览
收藏 0
回答 3
待解决
回答 3
按赞同
/
按时间
没用的喵叔
6

xml的方式搞不定。java代码的方式可以:

Button btn_rectangle = (Button) findComponentById(ResourceTable.Id_btn_rectangle);
btn_rectangle.setBackground(shape1());

private ShapeElement shape1(){
    ShapeElement shapeElement = new ShapeElement();
    shapeElement.setShape(ShapeElement.RECTANGLE);
    //color关键值
    RgbColor[] rgbColors = new RgbColor[]{
            RgbColor.fromArgbInt(getColor(ResourceTable.Color_btn_start)),
            RgbColor.fromArgbInt(getColor(ResourceTable.Color_btn_center)),
            RgbColor.fromArgbInt(getColor(ResourceTable.Color_btn_end))};
    shapeElement.setRgbColors(rgbColors);
    //线性变化:对应type="linear"
    shapeElement.setShaderType(ShapeElement.LINEAR_GRADIENT_SHADER_TYPE);
    //变化方向,从左到右:对应angle="0"(我理解的应该没错吧,你自己再调整一下)
    shapeElement.setGradientOrientation(ShapeElement.Orientation.LEFT_TO_RIGHT);
    return shapeElement;
}

{
  "color": [
    {
      "name": "btn_start",
      "value": "#91E344"
    },
    {
      "name": "btn_center",
      "value": "#68D33C"
    },
    {
      "name": "btn_end",
      "value": "#37C231"
    }
  ]
}

 

 

分享
微博
QQ
微信
回复1
2021-07-06 15:09:06
没用的喵叔
5

直接通过xml文件定义shape也是可以的。不过现在ide没有提示,估计还没有正式发布吧。

<?xml version="1.0" encoding="UTF-8" ?>
<shape
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:shape="rectangle">
    <corners ohos:radius="100vp"/>
    <solid
        ohos:colors="#91E344,#68D33C,#37C231"/>
    <gradient
        ohos:shader_type="linear_gradient"
        ohos:orientation="LEFT_TO_RIGHT"/>
</shape>
分享
微博
QQ
微信
回复4
2021-07-06 15:50:35
爱吃土豆丝的打工人
3

你这个里面的attr都不存在昂,不能用这种方式做。  刚刚也试了,gradient只有一个属性是

要不你还是用渐变图的方式实现吧。

分享
微博
QQ
微信
回复
2021-07-06 09:35:14
相关问题
Preferences不起作用
2387浏览 • 1回复 待解决
设置状态栏颜色不起作用怎么回事?
397浏览 • 1回复 待解决
hilogprivate参数不起作用
4269浏览 • 1回复 待解决
鸿蒙中资源文件都又什么作用
1874浏览 • 1回复 待解决
文字背景颜色渐变显示
304浏览 • 1回复 待解决
如何获取HSP资源文件
266浏览 • 1回复 待解决
鸿蒙 如何实现一个渐变圆形图片;
10500浏览 • 2回复 已解决
鸿蒙如何获取资源文件路径
13754浏览 • 3回复 待解决
自定义资源文件怎么读取?
269浏览 • 1回复 待解决
ArkTS如何自定义资源文件
314浏览 • 1回复 待解决
XComponent组件如何设置背景颜色
350浏览 • 1回复 待解决
JS 卡片背景颜色怎么修改呢?
4958浏览 • 3回复 待解决
Button组件如何设置渐变背景
365浏览 • 1回复 待解决
如何实现组件边缘颜色渐变
215浏览 • 1回复 待解决
一个HarmonyOS版本商城APP,学习用
920浏览 • 1回复 待解决
如何将背景颜色设置为透明
289浏览 • 1回复 待解决
如何编写打 nginx dockerfile 文件?
2100浏览 • 1回复 待解决