2012-04-13 2 views
0

원하는 비율로 뷰를 표시하도록 LinearLayout을 가져 오는 데 문제가 있습니다. LinearLayout 및 layout_weight에 대한 10 또는 15 가지 다른 질문을 읽었지만 어떤 이유로 응답이 제 경우에는 적용되지 않는 것 같습니다. 내가으로 만든 커스텀 뷰와 사용자 정의에 AlertDialog의 색상 선택기를 만드는 오전하면 다음과중첩 된 LinearLayout을 사용할 때 Layout_weight가 원하는대로 작동하지 않습니다.

private LinearLayout createLayout() { 
    //Create our top-level layout 
    LinearLayout mainLayout = new LinearLayout(mContext); 
    mainLayout.setOrientation(LinearLayout.VERTICAL); 
    mainLayout.setPadding(15, 0, 15, 0); 
    //Make the preset view at the top 
    LinearLayout.LayoutParams presetViewParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 0, 0.2f); 
    mainLayout.addView(new PresetView(mContext), presetViewParams); 
     //Create another linear layout 
     LinearLayout subLayout = new LinearLayout(mContext); 
     subLayout.setOrientation(LinearLayout.HORIZONTAL); 
     subLayout.setPadding(0, 15, 0, 15); 
     //Add the Sat Lum View to the second layout 
     LinearLayout.LayoutParams satLumViewParams = new LinearLayout.LayoutParams(0, LayoutParams.MATCH_PARENT, 0.60f); 
     subLayout.addView(new SatLumView(mContext), satLumViewParams); 
     //Add the divider between the sat lum view and the hue view 
     LinearLayout.LayoutParams dividerViewParams = new LinearLayout.LayoutParams(0, LayoutParams.MATCH_PARENT, 0.1f); 
     subLayout.addView(new DividerView(mContext), dividerViewParams); 
     //Add the Hue View to the second layout 
     LinearLayout.LayoutParams hueViewParams = new LinearLayout.LayoutParams(0, LayoutParams.MATCH_PARENT, 0.30f); 
     subLayout.addView(new HueView(mContext), hueViewParams); 
     //Add the second layout to the first layout 
     LinearLayout.LayoutParams subLayoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 0, 0.7f); 
     mainLayout.addView(subLayout, subLayoutParams); 
    //Add the Line view at the bottom of the main layout 
    LinearLayout.LayoutParams lineParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 0, 0.1f); 
    mainLayout.addView(new LineView(mContext), lineParams); 
    //Return our completed layout 
    return mainLayout; 
} 

나는 데 문제입니다 내 세 개의 수직 요소 (PresetView, subLayout 및 LineView)의 layout_height를 설정하는 데에도 불구하고 "0"으로 설정하면 가중치는 내가 원하는대로 사용 가능한 공간을 분배하지 못합니다. subLayout은 공간의 70 %를 차지하지 않지만 항상 적습니다. 내가 말할 수있는 것부터 PresetView는 요청할 때 항상 수직 공간을 많이 차지하고 있으며 subLayout과 LineView는 남은 부분을 차지합니다. 내가있는 LinearLayout은 크기를 제어하려는 때문에합니다 (PresetView는 차원 막 세트로 작동하도록 설계 -

250
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     int width = resolveSize(250, widthMeasureSpec); 
     int height = resolveSize(150, heightMeasureSpec); 
     setMeasuredDimension(width, height); 
    } 

150이 아니라 임의의 값입니다 : 여기 내 된 onMeasure PresetView에 대한() 메소드입니다!). 0의 layout_height를 사용하고 가중치를 지정하면 View를 제어하는 ​​View가 아닌 ​​View가 차지하는 공간을 제어 할 수있는 레이아웃을 적용한다는 인상하에있었습니다. PresetView가 layout_height를 0으로 설정했을 때 전체 150 픽셀을 차지하는 이유는 무엇입니까?

다른보기의 onMeasure() 메서드는 PresetView와 동일하며 다른 값만 사용합니다.

답변

1

나는 onMeasure()를 부적절하게 오버 라이딩 한 것처럼 보입니다. 보기가 미리 정의 된 측정 값 (250 x 150 픽셀이라고 알고 있음)이있는 경우 내가 사용한 경로가 유용합니다. 이가 layout_weight를 속성을 수 있습니다

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
    } 

: 어떤 화면 크기에 맞게 확장과 레이아웃, 내가 할 필요가 모든 의해 완전히 제어 할 수 내보기를 원하기 때문에, 기본 된 onMeasure() 구현을 사용했다 자식 뷰의 모든 크기 조정을 처리합니다.

관련 문제