有个小需求, 编码头部驻留得item, 其中就用到了, 生成默认得layoutparams得东西,
// read layout parameters
AbsListView.LayoutParams layoutParams = (LayoutParams) pinnedView.getLayoutParams();
if (layoutParams == null) {
layoutParams = this.generateDefaultLayoutParams();
pinnedView.setLayoutParams(layoutParams);
}
anroid5.0.1 的实现
@Override protected ViewGroup.LayoutParams generateDefaultLayoutParams() { return new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 0); }
anroid4.0的实现
/** * Returns a set of default layout parameters. These parameters are requested * when the View passed to {@link #addView(View)} has no layout parameters * already set. If null is returned, an exception is thrown from addView. * * @return a set of default layout parameters or null */ protected LayoutParams generateDefaultLayoutParams() { return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); }
也许你已经发现问题了, 低版本的generateDefaultLayoutParams生成的是ViewGroup的layoutparams, 如果你要强转成AbsListView.LayoutParams这个肯定是要报错得. 父亲怎么像儿子呢
时间: 2024-11-08 06:43:32