How to retrieve XML attribute for custom control

前端 未结 2 1218
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-19 16:49

I\'ve created a combo box control with a edittext and spinner. I\'m trying to let the android:prompt attribute be passed onto the spinner, which means I need to catch it i

相关标签:
2条回答
  • 2020-12-19 17:04
    1. Define a style in the xml. For ex : <declare-styleable name="ComboBox"> <attr name="prompt" format="reference"/> </declare-styleable>

    2. To get the value in the constructor use : TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ComboBox);

    Use the TypedArray get methods to get the particular attribute.

    0 讨论(0)
  • 2020-12-19 17:13

    I just wrote an answer explaining the whole process for using XML with custom UI elements. In your case, there is no need to declare a styleable, as you don't need custom attributes. Using android.R.attr.prompt as the int id will work fine. R.styleable.className_attributeName will only work if you defined your attributes in the styleable and you retrieved them by passing R.styleable.className into obtainStyledAttributes.

    0 讨论(0)
提交回复
热议问题