程序中初始化控件方法:(View)findViewById(int redID),而且还得强制转化为相应类型,code浪费大量的时间, 阅读起来也不美观。 //一般写法,很累跟强制转换似的,很难受这样写 tv_title_other = (TextView) findViewById(R.id.tv_title_other); //替代方式 protected <T extends View> T findView(int viewId) { return (T) findViewById(viewId); } //简化之后 tv_title_other = findView(R.id.tv_title_other);
代码片段
ViewGroup.java {
public ViewGroup(Context context, AttributeSet attrs) {
super((Context)null, (AttributeSet)null, 0, 0);
throw new RuntimeException("Stub!");
}
}
View.java {
public View(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
throw new RuntimeException("Stub!");
}
}