【问题标题】:What is the alternative in Harmony OS for TypedArray in Android?Harmony OS for Android 中 TypedArray 的替代方案是什么?
【发布时间】:2021-07-30 07:54:59
【问题描述】:

在我的项目中,我想检索索引处自定义属性的颜色值。就像在 android 中一样,我们有 TypedArray 来执行此功能。

final TypedArray typedArray = mContext.obtainStyledAttributes(attrs, R.styleable.CustomCalendarView, 0, 0);
calendarBackgroundColor = typedArray.getColor(R.styleable.CustomCalendarView_calendarBackgroundColor, getResources().getColor(R.color.white));
calendarTitleBackgroundColor = typedArray.getColor(R.styleable.CustomCalendarView_titleLayoutBackgroundColor, getResources().getColor(R.color.white));
calendarTitleTextColor = typedArray.getColor(R.styleable.CustomCalendarView_calendarTitleTextColor, getResources().getColor(R.color.black));
weekLayoutBackgroundColor = typedArray.getColor(R.styleable.CustomCalendarView_weekLayoutBackgroundColor, getResources().getColor(R.color.white));
typedArray.recycle();

所以我们在 Harmony 操作系统中没有 TypedArray。执行此功能的方式是什么?

【问题讨论】:

    标签: java android huawei-mobile-services huawei-developers harmonyos


    【解决方案1】:

    在 HarmonyOS 中,您不需要 TypedArray 从 AttributeSet 中检索自定义属性,而是 AttrSet API 直接允许您检索自定义属性。

        indicatorNormalColor = mAttrSet.getAttr(INDICATOR_NORMAL_COLOR).isPresent() ?
                mAttrSet.getAttr(INDICATOR_NORMAL_COLOR).get().getColorValue() : defaultIndicatorNormalColor;
        indicatorSelectedColor = mAttrSet.getAttr(INDICATOR_SELECTED_COLOR).isPresent() ?
                mAttrSet.getAttr(INDICATOR_SELECTED_COLOR).get().getColorValue() : defaultIndicatorSelectedColor;
        indicatorStrokeColor = mAttrSet.getAttr(INDICATOR_STROKE_COLOR).isPresent() ?
                mAttrSet.getAttr(INDICATOR_STROKE_COLOR).get().getColorValue() : defaultIndicatorStrokeColor;
        indicatorNormalStrokeWidth = mAttrSet.getAttr(INDICATOR_NORMAL_STROKE_WIDTH).isPresent() ?
                mAttrSet.getAttr(INDICATOR_NORMAL_STROKE_WIDTH).get().getDimensionValue() : defaultIndicatorNormalStrokeWidth;
        indicatorSelectedStrokeWidth = mAttrSet.getAttr(INDICATOR_SELECTED_STROKE_WIDTH).isPresent() ?
                mAttrSet.getAttr(INDICATOR_SELECTED_STROKE_WIDTH).get().getDimensionValue() : 
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多