我正在学习自定义组件,我在自定义xml属性方面遇到了一些麻烦.我的自定义组件扩展了LinearLayout并在构造函数中(公共自定义(Context context,AttributeSet attrs))我正在膨胀xml布局(2个按钮和1 EditText).我还在values / attrs中声明了这个自定义属性:
在我膨胀布局后的构造函数中,我正在尝试读取这样的自定义属性:
if (attrs != null) {
TypedArray ta = context.obtainStyledAttributes(attrs,
R.styleable.Custom, 0, 0);
setInitValue(ta.getInt(R.styleable.Custom_initValue, 0));
setStepSize(ta.getInt(R.styleable.Custom_stepSize, 1));
setMaxValue(ta.getInt(R.styleable.Custom_maxValue, 5));
ta.recycle();
}
现在我尝试通过将其添加到xml布局来测试此自定义组件,如下所示:
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
android:layout_width="wrap_content" android:layout_height="wrap_content"
initValue="2" maxValue="7" stepSize="1" />
这不起作用,我只得到默认值(0,1,5).我错过了什么或这是正常的行为?