这几天在开发一个Android上的应用程序,其中就用到了ExpandableListView控件,至于这个控件怎么用,我就不多说了,在网上一搜就有很多文章,我也是根据网上的文章学会的基本用法,但只会基本用法对于真正的开法来说,往往是不够用的,而我遇到的问题是让ExpandableListView的Item高度动态改变,当我像往常一样,用如下代码改变Item中view高度时,却怎么也编译不通过。
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { // TODO Auto-generated method stub LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); RelativeLayout lt= (RelativeLayout) layoutInflater.inflate(R.layout.child, null); LinearLayout lt= (LinearLayout) FoodActivity.this.findViewById(R.id.tittleID); lt.getLayoutParams().height =400; lt.getLayoutParams().width =300; return relativeLayout; }
在百思不得其解的时候,发现ExpandableListview是继承自AbsListView类的,这个类有个子类是LayoutParams,于是我就用AbsListView的子类LayoutParams定义了一个对象,指定其大小,然后再setLayoutParams()一下,竟然OK了,兴奋...代码如下:
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { // TODO Auto-generated method stub LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); RelativeLayout lt= (RelativeLayout) layoutInflater.inflate(R.layout.child, null); AbsLayoutParams.LayoutParams lp = new AbsLayoutParams.LayoutParams(400,300); lt.setLayoutParams(lp); return relativeLayout }