1 gridView = (GridView) getView().findViewById(R.id.gridView_musicbook); 2 3 gridView.getViewTreeObserver().addOnGlobalLayoutListener( 4 new OnGlobalLayoutListener() { 5 @Override 6 public void onGlobalLayout() { 7 // TODO Auto-generated method stub 8 mgridWidth = gridView.getWidth();//获取宽度//获取高度gridView.getHeight()
9 // getViewTreeObserver().removeGlobalOnLayoutListener(this); 10 int itemWidth = mgridWidth / (songFiles.IMAGE_HEIGHT); 11 if (itemWidth > 0) 12 gridView.setNumColumns(itemWidth); 13 } 14 });
横竖屏切换时会重新跑oncreate事件,所以屏幕切换时会重新计算高度