如何检索自定义控制XML属性自定义、属性、XML

2023-09-06 13:10:39 作者:ぁ千纸鹤たぼ

我已经创建了一个带有的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.

感谢

推荐答案

我只是写了一个答案解释整个过程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.

 
精彩推荐
图片推荐