如何检索自定义控制XML属性 [英] How to retrieve XML attribute for custom control

查看:122
本文介绍了如何检索自定义控制XML属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个带有的EditText和微调组合框控件。 我试图让机器人:提示属性被传递到 微调,这意味着我需要抓住它的构造函数 通过我的AttributeSet中和微调设置。 我无法弄清楚如何获得提示的值。 我想,

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 in the constructor which passes my the AttributeSet and set it on the spinner. I can't figure out how to get the value of the prompt. I'm trying,

int[] ra = { android.R.attr.prompt };
TypedArray ta = context.getTheme().obtainStyledAttributes(ra); 
int id = ta.getResourceId(0, 0); 

我回去0,这意味着它没有找到属性。 我也做了ta.count()的返回0,所以我没有得到任何东西。

I get back 0, which means it didn't find the attribute. I also did a ta.count() which returned 0. So I'm not getting anything back.

我的XML只定义一个Android:提示值

My XML simply defines an android:prompt value.

感谢

推荐答案

我只是写了一个答案解释整个过程<一href="http://stackoverflow.com/questions/2695646/declaring-a-custom-android-ui-element-using-xml/2695649">using XML定制UI元素的。在你的情况下,没有必要申报设置样式,因为你不需要自定义属性。使用 android.R.attr.prompt 作为INT ID将正常工作。 R.styleable.className_attributeName 如果你定义你的属性的设置样式和您检索它们传递将仅 R.styleable.className obtainStyledAttributes

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.

这篇关于如何检索自定义控制XML属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆