自定義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();
		 
	}


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