Attributes are a powerful way of controlling the behavior and appearance of views, but they can only be read when the view is initialized. To provide dynamic behavior, expose a property getter and setter pair for each custom attribute. The following snippet shows how PieChart exposes a property called showText:
publicbooleanisShowText(){returnmShowText;}publicvoidsetShowText(booleanshowText){mShowText=showText;invalidate();requestLayout();}
Notice that setShowText calls
Custom views should also support event listeners to communicate important events. For instance, PieChartexposes a custom event called OnCurrentItemChanged to notify listeners that the user has rotated the pie chart to focus on a new pie slice.
It's easy to forget to expose properties and events, especially when you're the only user of the custom view. Taking some time to carefully define your view's interface reduces future maintenance costs. A good rule to follow is to always expose any property that affects the visible appearance or behavior of your custom view. 五、
Design For Accessibility
Your custom view should support the widest range of users. This includes users with disabilities that prevent them from seeing or using a touchscreen. To support users with disabilities, you should:
Label your input fields using the android:contentDescription attribute
Send accessibility events by calling
Support alternate controllers, such as D-pad and trackball