03.鸿蒙HarmonyOS卡片 状态栏透明和代码设置渐变色

1.状态栏透明和去掉标题栏

  • 状态栏透明:
    WindowManager.getInstance().getTopWindow().get().addFlags(WindowManager.LayoutConfig.MARK_ALLOW_EXTEND_LAYOUT);
  • 去掉状态栏:
    getWindow().addFlags(WindowManager.LayoutConfig.MARK_FULL_SCREEN);
  • 去掉标题栏
 "abilities": [
	"metaData":{
          "customizeData":[
            {
              "name": "hwc-theme",
              "value": "androidhwext:style/Theme.Emui.Light.NoTitleBar",
              "extra": ""
            }
          ]
        }
]

更多状态栏和导航栏处理:玩转HarmonyOS 状态栏&标题栏&导航栏相关操作方法整理

2.代码设置渐变色shape


由于看到不能在shape直接设置渐变,所以找到一个公共方法去设置:

    /**
     * 渐变色背景
     */
    public static ShapeElement getButtonShape(AbilityContext context, float radius, int resStartId, int resEndId) {
        ShapeElement shapeElement = new ShapeElement();
        shapeElement.setCornerRadius(radius);
        shapeElement.setShape(ShapeElement.RECTANGLE);
        //color关键值
        RgbColor[] rgbColors = new RgbColor[]{
                RgbColor.fromArgbInt(context.getColor(resStartId)),
                RgbColor.fromArgbInt(context.getColor(resEndId))};
        shapeElement.setRgbColors(rgbColors);
        //线性变化:对应type="linear"
        shapeElement.setShaderType(ShapeElement.LINEAR_GRADIENT_SHADER_TYPE);
        //变化方向,从左到右:对应angle="0"
        shapeElement.setGradientOrientation(ShapeElement.Orientation.LEFT_TO_RIGHT);
        return shapeElement;
    }

    /**
     * 通过id获取View
     */
    public static <T extends Component> T findById(AbilitySlice context, int id) {
        return (T) context.findComponentById(id);
    }

使用:

Button btn_rectangle = (Button) findComponentById(ResourceTable.Id_bt_search_button);
btn_rectangle.setBackground(ViewUil.getButtonShape(this, 54, ResourceTable.Color_btn_start_search, ResourceTable.Color_btn_end_search));

发现没有color文件,创建一个color.json文件:
创建color.json
color
得到对应的color文件:

{
  "color": [
    {
      "name": "btn_start_search",
      "value": "#D6A2FF"
    },
    {
      "name": "btn_end_search",
      "value": "#AC69FD"
    },
    {
      "name": "color_868686",
      "value": "#868686"
    }
  ]
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值