2014-03-24 3 views
0

프로그래밍 방식으로 TextView를 만든 후에 높이를 찾는 데 도움이 필요합니다. 다른 TextViews에서 텍스트가 진행되지 않도록 내가 알고프로그래밍 방식으로 생성 된 TextView의 높이 찾기

TextView text = drawTextView(map.get("business"), false, true, topMargin, 30, 40); 

topMargin += text.getHeight() + 5; 

text = drawTextView(map.get("text"), false, true, topMargin, 30, 16); 

topMargin += text.getHeight() + 5; 

text = drawTextView(map.get("detail"), false, false, topMargin, 30, 16); 

topMargin += text.getHeight() + 5; 

: 내가 할 수 있도록하고 싶습니다 무엇

public TextView drawTextView(String text, boolean center, boolean bold, int topMargin, int leftMargin, int textSize) { 
    View vt = new TextView(getBaseContext()); 
    final TextView textView = new AutoResizeTextView(vt.getContext()); 

    Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/GothamMedium.ttf"); 

    textView.setText(text); 
    textView.setTextColor(0xFFFFFFFF); 
    if (bold) { 
     textView.setTypeface(tf, Typeface.BOLD); 
    } else { 
     textView.setTypeface(tf); 
    } 
    if (center) { 
     textView.setGravity(Gravity.CENTER); 
    } 
    textView.setTextSize(textSize); 
    textView.setSingleLine(false); 

    LayoutParams paramsText = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
    paramsText.leftMargin = leftMargin; 
    paramsText.topMargin = topMargin; 

    container.addView(textView, paramsText); 

    return textView; 
    } 

이 같은 것입니다 :이 같은 텍스트 뷰를 생성 오버랩하려면 텍스트가 한 줄의 텍스트보다 높게 끝나고 다른 휴대폰에서 다른 TextView 크기를 고려해야 할 경우에도 항상 5 픽셀의 차이가 있습니다. 그러나 text.getHeight()는 항상 0을 반환합니다.이 문제를 해결하기 위해 할 수있는 일이 있습니까? 솔루션 전체를 살펴 봤지만 찾지 못했습니다. 어떤 도움이라도 대단히 감사하겠습니다.

+0

을 수행 할 수 있습니다 (에서 onCreate에, 폭과 높이가 예를 들어 0이다, (그린) 아직) 메소드가 아직 그려지지 않았습니다. 예를 들어 OnGlobalLayoutListener의 onGlobalLayout() 메서드와 같이보기 자체가 배치 될 때보기의 높이를 사용해야합니다. 일부는 [여기]에서 찾을 수 있습니다 (http://stackoverflow.com/questions/4142090/how-do-you-to-retrieve-dimensions-of-a-view-getheight-and-getwidth-always-r?rq= 1) – Onik

답변

0

절대 위치에서 수동으로 그리기보다는 세로 형 LinearLayout을 사용하는 것이 좋습니다. 각 TextView의 위쪽 여백을 5 픽셀로 설정할 수 있습니다. 그것은이 같은 것을 볼 수 있었다 : 뷰가 밖으로 누워되지 않은 경우

LinearLayout linearLayout = (LinearLayout)findViewById(R.id.textContainer); //Or make one programmatically 
TextView textView = ...; //Your creation code from above plus layout params with 5 pixel top margin 
linearLayout.addView(textView); 
0

당신은

yourView.getLayoutParams().height 
관련 문제