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"
    }
  ]
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.

 

 

分享
微博
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>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
分享
微博
QQ
微信
回复4
2021-07-06 15:50:35
爱吃土豆丝的打工人
3

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

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

分享
微博
QQ
微信
回复
2021-07-06 09:35:14
相关问题
HarmonyOS zIndex不起作用
734浏览 • 1回复 待解决
Preferences不起作用
4886浏览 • 1回复 待解决
HarmonyOS 清除缓存不起作用
637浏览 • 1回复 待解决
设置状态栏颜色不起作用怎么回事?
3005浏览 • 1回复 待解决
hilogprivate参数不起作用
6523浏览 • 1回复 待解决
HarmonyOS异步await不起作用
1014浏览 • 1回复 待解决
HarmonyOS column设置圆角不起作用
1516浏览 • 1回复 待解决
HarmonyOS router.back不起作用
692浏览 • 1回复 待解决
HarmonyOS bindSheet设置preferType不起作用
461浏览 • 1回复 待解决
HarmonyOS TextAlign.JUSTIFY 不起作用
1011浏览 • 1回复 待解决
HarmonyOS data_preferences remove不起作用
970浏览 • 1回复 待解决
鸿蒙中资源文件都又什么作用
4376浏览 • 1回复 待解决
文字背景颜色渐变显示
2873浏览 • 1回复 待解决