自定义viewgroup里面嵌套viewgroup布局出问题,如fill_parent失效

我的一个新项目用到了一个自定义可以拖动的控件,我在网上找到了一个demo,是通过重写viewgroup来实现的,但是当我真正投入

使用的时候,发现我在里面自己写的布局fill_parent失效,找了一上午终于发现问题,没有重写viewgroup里面的onMeasure方法,

重写之后发现还是没有用,原来在我的onlayout方法里面调用了子view 的measure方法。下面直接上部分代码了。

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
	// TODO Auto-generated method stub
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);

		final int count = getChildCount();
		for (int i = 0; i < count; i++) { 
 			getChildAt(i).measure(widthMeasureSpec, heightMeasureSpec);
		}

	}

	/**
	 * 滑动过后,只要手指离开屏幕,就会调用这个方法,这个其实是在调整子view
	 */
	@Override
	protected void onLayout(boolean changed, int left, int top, int right,int bottom) {
		 Log.i(TAG, ">>left: " + left + " top: " + top + " right: " + right  
	                + " bottom:" + bottom);  
		 for(int i=0;i<getChildCount();i++){
			 View child = getChildAt(i);
			 child.setVisibility(View.VISIBLE);
		//	 child.measure(right-left, bottom-top);	//可以看成是在设置view的大小  ,把这一行去掉就好了
			 child.layout(0 + i * getWidth(), 0, getWidth() + i * getWidth(),  
	                    getHeight());
			 
 		 }
		 
		 int delta = currentScreenIndex*getWidth()-getScrollX();
		 scroller.startScroll(getScrollX(),0,delta,0, 500);
		 invalidate();
		 
	}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章