Attention!
If you set attributes with app:attribute="value" in XML, you lose their values ββwhen reading with:
@Override protected void onFinishInflate() { super.onFinishInflate(); unbinder = ButterKnife.bind(this); TypedValue typedValue = new TypedValue(); TypedArray typedArray = getContext().obtainStyledAttributes(typedValue.data, R.styleable.YourStyleable); try { int number = typedArray.getResourceId(R.styleable.YourStyleable_number, 0); image.setImageResource(number); String text = typedArray.getString(R.styleable.YourStyleable_text); text.setText(text); } finally { typedArray.recycle(); } }
Their values ββwill be 0 and null. Initialize them in the custom view designer.
The reason is to use obtainStyledAttributes(typedValue.data instead of obtainStyledAttributes(attrs .
See: Magic with the getStyledAttributes method .
Coolmind
source share