0

在我的项目中,我想检索索引处自定义属性的颜色值。就像在 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();

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

4

1 回答 1

0

在 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() : 

于 2021-07-30T09:14:51.260 回答