自定义 View 的属性传递注意项 Android custom view deliver variable tips
<declare-styleable name="ExampleView">
<attr name="maxChar" format="integer"/>
<attr name="android:textSize"/>
</declare-styleable>
<declare-styleable name="ExampleView">
<attr name="android:textSize"/>
</declare-styleable>
取值时应该:
TypedArray typedArray = context.obtainStyledAttributes(attributeSet, /*定义的 styleable 文件 ID*/null);
int textSize = attributes.getDimensionPixelSize(R.styleable.ExampleView_android_textSize, 16);
使用时应该:
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);