英文:
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.getDimensionPixelSize
、styleArray.getColor
和styleArray.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(...)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论