2012-09-11 2 views
7

이 내 코드가 FrameLayout 함께 표시되지 않습니다, FrameLayout이 확장.는 아이 뷰는 더 이상

이제 FrameLayout에서 맞춤 레이아웃을 확장했습니다. MyFrameLayout.

는 MyFrameLayout, 나는 레이아웃의 높이가 항상 폭의 절반 원하는, 그래서 내 코드는 지금 내부

<com.test.MyFrameLayout 
     android:layout_width="fill_parent" android:layout_height="wrap_content"> 
    <ImageView 
      android:id="@+id/frameView" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:src="@drawable/image1" 
      /> 
</com.test.MyFrameLayout> 

그러나 그런 다음

public class MyFrameLayout extends FrameLayout { 

    // 3 constructors just call super 

    @Override 
    protected void onMeasure(int widthMeasureSpec, 
         int heightMeasureSpec) { 
     int width = MeasureSpec.getSize(widthMeasureSpec); 
     int height = (int) (width * 0.5); 
     setMeasuredDimension(width, height); 
    } 
} 

XML에서 사용 ImageView가 사라졌습니다.

onMeasure 또는 onLayout을 올바르게 구현하지 않은 것 같습니다. 아이들 뷰도 크기를 조정해야한다고 생각합니다. 그러나 나는 지금 무엇을 해야할지 모른다.


UPDATE TheDimasig의 코멘트 당

은, 난 그냥 내 코드를 확인하고 내 질문을 업데이트합니다. 감사합니다

+0

두 가지 제안 : 1. MyFrameLayout 클래스에서 onMeasure와 onLayout을 확인하십시오. 2. 디버깅을 위해 hierarhyviewer 도구를 사용해보십시오. 또한, 사용자 정의 레이아웃 –

+0

@TheDimasig의 모든 코드를 표시 할 수 있다면, 제 질문을 업데이트했습니다. 리뷰 : – Freewind

답변

2

나는 onMeasure 메서드에서 ImageView를 포함한 모든 하위 뷰에서 onMeasure를 호출하는 것을 잊어 버렸습니다. 올바르게 방법 onMeasure를 오버라이드 (override)하지 않기 때문에 당신은 어떤 아이 뷰가 표시되지

for (int i = 0; i < getChildCount(); i++) { 
     getChildAt(i).measure(....); 
    } 
+0

'FrameLayout'의 소스를 읽었을 때,'onMeasure'와'onLayout'는 꽤 복잡해 보입니다. 이 작업을 수행하는 간단한 방법이 있습니까? – Freewind

+0

대신 모든 메서드 본문과 setDimension()을 재정 의하여 params로 super.onMeasure (widthMeasureSpec * 0.5, heightMeasureSpec)를 호출하십시오. 이 솔루션은 onLayout을이 방법으로 오버라이드해야 할 수도 있습니다. –

+0

'widthMeasureSpec * 0.5'을 시도했지만 최종 크기가 예상과 다르게 보입니다. 나는 그것을 얻을 수 없다, 당신은 나에게 시간이있을 때 일하는 모범을 줄 수 있습니까? – Freewind

7

: 당신은 당신의 아이 뷰를 통해 가서 그들에게 이런 일을 된 onMeasure를 호출해야합니다. 당신은 슈퍼 클래스는 로직을 구현 할 수있다 (그래서 아이 뷰가 표시됩니다) 다음 MyFrameLayout에 대한 계산 된 폭과 높이 재설정 : 당신이 게시 그 레이아웃이 다음 내 코드 전체 레이아웃이 아닌 경우

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    // let the super class handle calculations 
    super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
    // set again the dimensions, this time calculated as we want 
    setMeasuredDimension(getMeasuredWidth(), getMeasuredHeight()/2); 
    // this is required because the children keep the super class calculated dimensions (which will not work with the new MyFrameLayout sizes) 
    final int count = getChildCount(); 
    for (int i = 0; i < count; i++) { 
     final View v = getChildAt(i); 
     // this works because you set the dimensions of the ImageView to FILL_PARENT   
     v.measure(MeasureSpec.makeMeasureSpec(getMeasuredWidth(), 
       MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(
       getMeasuredHeight(), MeasureSpec.EXACTLY)); 
    } 
} 

을 작동하지 않을 수 있습니다.

14

가장 간단한 방법은 된 onMeasure에서 measurespec을 변경하는 것입니다,하지만 여전히 슈퍼 전화 :

@Override 
protected void onMeasure(int widthMeasureSpec, 
        int heightMeasureSpec) { 
    int width = MeasureSpec.getSize(widthMeasureSpec); 
    int height = (int) (width * 0.5); 
    heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY); 
    super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
} 

원하는하지만 수동으로 아이들을 측정 할 수없는 크기를 얻을이 방법.

+1

나는 그것이 작동하는지 확인할 수있다. super.onMeasure()를 호출하지 않으면 하위 뷰가 표시되지 않았습니다. –

+0

정말 고맙습니다. 토론을 많이 읽었습니다.이 레이아웃은 하위 레이아웃을 부모 레이아웃으로 조정할 수있는 유일한 유효한 대답입니다! – Giova