如何使用 TypedArray.getResourceId() 方法返回的值?

huangapple go评论77阅读模式
英文:

How to use TypedArray.getResourceId() method returned value?

问题

public MultiButton(Context context, AttributeSet attrs) {
    TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MultiButton);
    int resId = typedArray.getResourceId(R.styleable.MultiButton_buttons_style, -1);

    if (resId != -1) {
        // Obtain a TypedArray for the style
        TypedArray styleArray = context.obtainStyledAttributes(resId, new int[] {
            android.R.attr.textSize,
            android.R.attr.textColor,
            android.R.attr.background
        });

        // Retrieve attributes from the style
        int textSize = styleArray.getDimensionPixelSize(0, -1);
        int textColor = styleArray.getColor(1, -1);
        Drawable background = styleArray.getDrawable(2);

        styleArray.recycle(); // Don't forget to recycle the TypedArray
    }

    typedArray.recycle(); // Don't forget to recycle the TypedArray
}

请注意,以上的代码是对您提供的原始代码的翻译和补充。在获取样式资源的TypedArray之后,我们使用styleArray.getDimensionPixelSizestyleArray.getColorstyleArray.getDrawable来检索样式中的属性值。最后,不要忘记使用recycle方法回收TypedArray,以防止资源泄漏。

英文:

I'm learning about Custom View, and i created a Styleable node for, one of the attribute is from a reference type, that is set in the xml file to a style. I don't know how to retrieve attributes values from the this style.
Styleable:

Styleable:

    <resources>
        <declare-styleable name="MultiButton">
            <attr name="numOfButtons" format="integer" />
            <attr name="activeButtonIndex" format="integer" />
            <attr name="backgroundSelector" format="reference"/>
            <attr name="buttons_style" format="reference"/>
        </declare-styleable>
   </resources>

Style:

<style name="MultiButtonStyle">
    <item name="android:textSize">14dp</item>
    <item name="android:textColor">@android:color/holo_purple</item>
    <item name="android:background">@android:color/holo_blue_dark</item>
</style>

xml:

	<com.example.currencyconvertor.MultiButton
    android:id="@+id/multiButton"
    android:layout_width="300dp"
    android:layout_height="50dp"
    app:activeButtonIndex="0"
    app:backgroundSelector="@drawable/multi_button_background_selector"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:numOfButtons="2"
    buttons_style="@style/MultiButtonStyle />

Code:

public MultiButton(Context context, AttributeSet attrs )
{
    TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.MultiButton);
    int resId = typedArray.getResourceId(R.styleable.MultiButton_buttons_style, -1));
}

I want to get all attributes that are defined under the style i have set for buttons_style attributes.
Now, i don't understand/know what to write in after setting the resId variable in order to get it.

Answer that was suggested a duplicate question doesn't refer to reference type, so getResoueceId method isn't explain there

答案1

得分: 1

typedArray.getResourceId(...)方法的中文翻译是:

typedArray.getResourceId(...)方法的中文翻译是:typedArray.getResourceId(...)

英文:

Please, try the following code and see if it helps you:

TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.MultiButton);
int resId = typedArray.getResourceId(R.styleable.MultiButton_buttons_style, THE_DEDAULT_RES_ID));

typedArray.getResourceId(...)

huangapple
  • 本文由 发表于 2020年8月16日 19:16:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/63436201.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定