2013-07-25 2 views
4

LinearLayout을 확장하는 사용자 지정보기가 있습니다. 이 커스텀 뷰는 LinearLayout과 똑같은 레이아웃을 가져야 만하는 몇 가지 다른 뷰를 포함하고 있습니다. 그러나 나는 그것들을 올바르게 레이아웃 할 수 없었습니다 ... 모든 서브 뷰는 이전에 추가 된 모든 서브 뷰를 숨기고 서로 위에 놓습니다.LinearLayout을 확장하는 사용자 레이아웃에 대한 Onlayout 메서드

내 onLayout과 된 onMeasure 다음과 같이

@Override 
protected void onLayout(boolean changed, int l, int t, int r, int b) { 
    // Do nothing. Do not call the superclass method--that would start a layout pass 
    // on this view's children. PieChart lays out its children in onSizeChanged(). 
    super.onLayout(changed, l, t, r, b); 
    Log.e(LOG_TAG, LOG_TAG + ".onLayout: " + l + ", " + t + ", " + r + ", " + b); 

    int iChildCount = this.getChildCount(); 
    for (int i = 0; i < iChildCount; i++) { 
     View pChild = this.getChildAt(i); 
     pChild.layout(l, t, pChild.getMeasuredWidth(), pChild.getMeasuredHeight()); 
    } 
} 

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    // Try for a width based on our minimum 
    super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
    Log.d(LOG_TAG, LOG_TAG + ".onMeasure: width: " + widthMeasureSpec + " getWidth: " + MeasureSpec.getSize(widthMeasureSpec)); 
    Log.d(LOG_TAG, LOG_TAG + ".onMeasure: height: " + heightMeasureSpec + " getHeight: " + MeasureSpec.getSize(heightMeasureSpec)); 
    Log.d(LOG_TAG, LOG_TAG + ".onMeasure: getPaddingLeft: " + getPaddingLeft() + " getPaddingRight: " + getPaddingRight()); 
    Log.d(LOG_TAG, LOG_TAG + ".onMeasure: getPaddingTop: " + getPaddingTop() + " getPaddingBottom: " + getPaddingBottom()); 

    // http://stackoverflow.com/a/17545273/474330 
    int iParentWidth = MeasureSpec.getSize(widthMeasureSpec); 
    int iParentHeight = MeasureSpec.getSize(heightMeasureSpec); 

    this.setMeasuredDimension(iParentWidth, iParentHeight); 

    int iChildCount = this.getChildCount(); 
    for (int i = 0; i < iChildCount; i++) { 
     View pChild = this.getChildAt(i); 
     this.measureChild(pChild, 
       MeasureSpec.makeMeasureSpec(iParentWidth, MeasureSpec.EXACTLY), 
       MeasureSpec.makeMeasureSpec(iParentHeight, MeasureSpec.EXACTLY) 
     ); 
    } 
} 

어떻게 내 사용자 지정보기의 폭과 높이의 x 위치, Y 위치를 설정합니까? 내 사용자 지정보기의 LayoutParam을 WRAP_CONTENT로 설정했지만 여전히 FILL_PARENT처럼 작동하며 부모에서 사용 가능한 모든 여유 공간을 차지합니다. 위치 나 크기를 변경하려는 모든 노력이 전혀 효과가없는 것처럼 보입니다. (심지어 위치를 제어하려고 시도해도 포장을하려고 시도했습니다.)

답변

1

나는 잠시 동안이 같은 문제로 고생했습니다. 그것이 부탁 받았던 이래로 그것은 긴 시간 인 것처럼 보인다. 그러나 내가 여기에서 그것이 일하기 위해 무엇을했는지에 관해 여기에서있다. 어쩌면 누군가가 줄을서는 것을 도울 것입니다.

LinearLayout 확장은 LinearLayout과 같은 방식으로 자식 뷰를 표시하려는 경우 onLayout을 재정의 할 필요가 없음을 의미합니다. 이 레이아웃을 예상대로 가져 오려면 내 onLayout 메서드를 제거하고 LinearLayout 클래스에서 처리하도록해야했습니다.

0

layout() 메서드의 세 번째와 네 번째 매개 변수는 너비와 높이가 아닌 "부모와 관련된 상대적인 위치"와 "부모와 관련된 상대적인 위치"입니다.

관련 문제