自定義view

自定義view中可以通過onMeasure拿到定義的尺寸
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		// TODO Auto-generated method stub
		width = measureDimension(220, widthMeasureSpec);
		height = measureDimension(290, heightMeasureSpec);
		setMeasuredDimension(width, height);
	}

	private int measureDimension(int defaultsize, int measureSpec) {
		int result = 0;
		int specMode = MeasureSpec.getMode(measureSpec);
		int specSize = MeasureSpec.getSize(measureSpec);
		if (specMode == MeasureSpec.EXACTLY) {
			result = specSize;
		} else if (specMode == MeasureSpec.AT_MOST) {
			result = Math.min(0, specSize);
		} else {
			result = 0;
		}
		return result;
	}

也可以通過getDrawingRect()方法拿到,但是會有負值,暫時未深入研究
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章