美文网首页
鸿蒙学习知识点总结(持续更新)

鸿蒙学习知识点总结(持续更新)

作者: mevenlu | 来源:发表于2022-04-07 15:47 被阅读0次

    Media资源转element

    public static PixelMapElement mediaResToPixel(Context context, int resId) {
        try {
            Resource resource = context.getResourceManager().getResource(resId);
            ImageSource source = ImageSource.create(resource, null);
            ImageSource.DecodingOptions opts = new ImageSource.DecodingOptions();
            opts.desiredSize = new Size(48,48);
            PixelMap pixelMap = source.createPixelmap(opts);
            return new PixelMapElement(pixelMap);
        } catch (IOException | NotExistException e) {
            e.printStackTrace();
        }
        return null;
    }
    

    ScrollView嵌套ListContainer

    ScrollView嵌套ListContainer

    使用 Fraction

    // 获取存储的状态,防止页面重叠
    @Override
    public void onRestoreAbilityState(PacMap inState) {
        super.onRestoreAbilityState(inState);
        int index = inState.getIntValue("stateTag", 0);
        changeFraction(index);
    }
    
    @Override
    public void onSaveAbilityState(PacMap outState) {
        outState.putIntValue("stateTag", selectIndex);
        super.onSaveAbilityState(outState);
    }
    
    //隐藏Fraction
    private void hideFraction(FractionScheduler fractionScheduler) {
        if (homeFraction != null) fractionScheduler.hide(homeFraction);
        if (matchFraction != null) fractionScheduler.hide(matchFraction);
        if (mineFraction != null) fractionScheduler.hide(mineFraction);
    }
    
    // 显示Fraction
    private void changeFraction(int index) {
        selectIndex = index;
        FractionScheduler scheduler = fManager.startFractionScheduler();
        hideFraction(scheduler);
        switch (index) {
            case 0:
                if (homeFraction == null) {
                    homeFraction = new HomeFraction();
                    scheduler.add(ResourceTable.Id_fraction_main, homeFraction);
                } else {
                    scheduler.show(homeFraction);
                }
                break;
            case 1:
                if (matchFraction == null) {
                    matchFraction = new MatchFraction();
                    scheduler.add(ResourceTable.Id_fraction_main, matchFraction);
                } else {
                    scheduler.show(matchFraction);
                }
                break;
            case 2:
                if (mineFraction == null) {
                    mineFraction = new MineFraction();
                    scheduler.add(ResourceTable.Id_fraction_main, mineFraction);
                } else {
                    scheduler.show(mineFraction);
                }
                break;
        }
        scheduler.submit();
    }
    

    隐藏 默认 title

    "metaData": {
          "customizeData": [
            {
              "name": "hwc-theme",
              "value": "androidhwext:style/Theme.Emui.Light.NoTitleBar"
            }
          ]
        }
    

    设置沉浸式状态栏

    getWindow().addFlags(WindowManager.LayoutConfig.MARK_TRANSLUCENT_STATUS);
    WindowManager.getInstance().getTopWindow().get().addFlags(WindowManager.LayoutConfig.MARK_ALLOW_EXTEND_LAYOUT);

    设置状态栏颜色

    WindowManager.getInstance().getTopWindow().get().setStatusBarColor(Color.BLUE.getValue());

    设置导航栏颜色

    WindowManager.getInstance().getTopWindow().get().setNavigationBarColor(Color.GREEN.getValue());

    设置沉浸式导航栏

    在Ability或者 AbilitySlice中添加如下代码(有如下几种方式):

    • getWindow().addFlags(WindowManager.LayoutConfig.MARK_TRANSLUCENT_NAVIGATION);
    • WindowManager.getInstance().getTopWindow().get().addFlags(WindowManager.LayoutConfig.FIRST_SYSTEM_WINDOW);
    • WindowManager.getInstance().getTopWindow().get().addFlags(WindowManager.LayoutConfig.MOD_STATUS_BAR);

    设置显示状态栏

    WindowManager.getInstance().getTopWindow().get().setStatusBarVisibility(Component.VISIBLE);

    设置隐藏状态栏

    • getWindow().addFlags(WindowManager.LayoutConfig.MARK_FULL_SCREEN);
    • WindowManager.getInstance().getTopWindow().get().setStatusBarVisibility(Component.INVISIBLE);
    • WindowManager.getInstance().getTopWindow().get().addFlags(WindowManager.LayoutConfig.MARK_FULL_SCREEN);

    获取屏幕宽度

    public static int getDisplayWidthInPx(Context context) {
        Display display = DisplayManager.getInstance().getDefaultDisplay(context).get();
        Point point = new Point();
        display.getSize(point);
        return (int) point.getPointX();
    }
    

    获取屏幕高度,不包含状态栏的高度

    public static float getDisplayHeightInPx(Context context) {
        Display display = DisplayManager.getInstance().getDefaultDisplay(context).get();
        Point point = new Point();
        display.getSize(point);
        return point.getPointY();
    }
    

    含状态栏的高度

    private float getRealHeight(Context context) {
        Point point = new Point();
        DisplayManager.getInstance().getDefaultDisplay(context).get().getRealSize(point);
        float height = point.getPointY();
        return height;
    }
    

    修改输入框大小和样式

    ohos:element_cursor_bubble="$graphic:ele_cursor_bubble"
    ohos:bubble_height="$float:vp_10"
    ohos:bubble_width="$float:vp_10"

    相关文章

      网友评论

          本文标题:鸿蒙学习知识点总结(持续更新)

          本文链接:https://www.haomeiwen.com/subject/dhjajrtx.html