How to retrieve XML attribute for custom control

此生再无相见时 提交于 2019-11-29 11:18:14
Casebash

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.

  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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!