访问小部件的内部属性
Accesing internal attributes for Widgets
我知道在创建自定义小部件时可以像这样访问小部件的自定义属性:
TypedArray a = context.getTheme().obtainStyledAttributes(
attributeSet,
R.styleable.FormEditText,
0, 0);
try {
leftIcon = a.getDrawable(R.styleable.FormEditText_leftIcon);
} finally {
a.recycle();
}
但是如果我想访问android:hint
,我如何在创建新的自定义视图时引用它?
谢谢!
如果您想在自定义视图中访问现有的 android:hint
属性,您只需创建一个新的样式并引用现有的 android 属性:
attrs.xml:
<declare-styleable name="YourView">
<attr name="android:hint" />
</declare-styleable>
在您的 customViews 初始化中,您可以获得这样的属性:
TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.YourView);
CharSequence hint = a.getText(R.styleable.YourView_android_hint)
a.recycle();
我知道在创建自定义小部件时可以像这样访问小部件的自定义属性:
TypedArray a = context.getTheme().obtainStyledAttributes(
attributeSet,
R.styleable.FormEditText,
0, 0);
try {
leftIcon = a.getDrawable(R.styleable.FormEditText_leftIcon);
} finally {
a.recycle();
}
但是如果我想访问android:hint
,我如何在创建新的自定义视图时引用它?
谢谢!
如果您想在自定义视图中访问现有的 android:hint
属性,您只需创建一个新的样式并引用现有的 android 属性:
attrs.xml:
<declare-styleable name="YourView">
<attr name="android:hint" />
</declare-styleable>
在您的 customViews 初始化中,您可以获得这样的属性:
TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.YourView);
CharSequence hint = a.getText(R.styleable.YourView_android_hint)
a.recycle();