2017-01-18 8 views
0

LinearLayout의 높이가 wrap_content으로 설정되고 자식의 결합 된 높이가 화면보다 큰 경우 일부 자식은 보이지 않습니다. 이 경우 모든 어린이가 보일 때까지 어린이를 삭제하고 싶습니다. 이 작업을 수행하는 효율적인 방법은 무엇입니까?LinearLayout이 너무 작아서 자식을 포함 할 수 없는지 확인하는 방법

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    //when measuring with unspecified, the layout will be as large as it wants to be 
    super.onMeasure(UNSPECIFIED_SPEC, UNSPECIFIED_SPEC); 
    if (getOrientation() == VERTICAL){ 
     wantedLength = getMeasuredHeight(); 

    } else { 
     wantedLength = getMeasuredWidth(); 
    } 

    super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
    if (getOrientation() == VERTICAL){ 
     measuredLength = getMeasuredHeight(); 

    } else { 
     measuredLength = getMeasuredWidth(); 
    } 


    while (measuredLength < wantedLength){ 
     //todo remove child 
     super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
     if (getOrientation() == VERTICAL){ 
      measuredLength = getMeasuredHeight(); 

     } else { 
      measuredLength = getMeasuredWidth(); 
     } 
    } 

} 

두 번 측정하지 않고 레이아웃이 너무 작 았는지 여부를 알 수있는 방법이 있습니까? 나는 문서에서 MEASURED_STATE_TOO_SMALL을 찾았지만 그것을 사용하는 방법을 모른다. https://developer.android.com/reference/android/view/View.html#getMeasuredHeightAndState()

+0

왜 모든 것을 보관하기 위해 'scrollview'를 사용하지 않습니까? 어쨌든'Display' 클래스로부터 디스플레이 크기를 얻을 수 있습니다. 그런 다음 항목의 총 크기를 계산하십시오. 다른 항목을 제거하십시오. –

+0

스크롤하지 않고 1 화면에 정보를 표시하고 싶습니다. 아이들이 적합하지 않을 때 덜 중요한 것들은 제거 될 수 있습니다. –

답변

0

는 픽셀 당신을 얻을)

public Map<String, Integer> deriveMetrics(Activity activity) { 
    try { 
     DisplayMetrics metrics = new DisplayMetrics(); 

     if (activity != null) { 
      activity.getWindowManager().getDefaultDisplay().getMetrics(metrics); 
     } 

     Map<String, Integer> map = new HashMap<String, Integer>(); 
     map.put("screenWidth", Integer.valueOf(metrics.widthPixels)); 
     map.put("screenHeight", Integer.valueOf(metrics.heightPixels)); 
     map.put("screenDensity", Integer.valueOf(metrics.densityDpi)); 

     return map; 
    } catch (Exception err) { 
     ; // just use zero values 
     return null; 
    } 
} 

다음 getWidth()와 getHeight (와 viewName.getParent()와 를 호출하여 뷰의 부모를 얻을 수있는 픽셀의 디스플레이 크기를 가져옵니다 필요한 것.

그런 다음 어느 것을 유지할 것인지 그리고 어떤 것을 제거 할 것인지 계산하십시오.