AbsListView的Header不能直接固定大小为0

 

直接上代码:

LinearLayout mLayout = new LinearLayout(this);
LayoutInflater.from(this).inflate(R.layout.header, mLayout, true);
mListView.addHeaderView(mLayout); 

如果设置(在Layout画出来之后):

LayoutParams lp = mLayout.getLayoutParams();
lp.height = 0;
mLayout.setLayoutParams(lp);

则mLayout的高度不是0,会变为WRAP_CONTENT。

所以,需要再加一层Layout。

mLayout = new LinearLayout(this);
LinearLayout container = new LinearLayout(this);
LayoutInflater.from(this).inflate(R.layout.header, mLayout, true);
container.addView(mLayout);
mListView.addHeaderView(container);

这样才能正常将mLayout的高度设置为0。

猜你喜欢

转载自jameszhao84.iteye.com/blog/2186329