2013-05-22 3 views
0

나는 TextView이 레이아웃을 초과 할 때까지 TextView, 문자열을 넣기를 원했을 때 그 LinearLayoutScrollView으로 만들었습니다. 문제는 루프가 끝나지 않는 동안 내 코드입니다.증가하는 scrollview의 크기를 관리하는 방법은 무엇입니까?

public class MainActivity extends Activity { 
public static int screenWidth,screenHeight; 
public boolean overlap; 


@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main) ; 



    ScrollView scroll=(ScrollView) findViewById(R.id.scrollView1); 
    TextView mytextview=(TextView) findViewById(R.id.textview1); 
    TextView textshow=(TextView) findViewById(R.id.textView2); 
    LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearlayout); 

    mytextview.setText(""); 

    ViewTreeObserver vto=scroll.getViewTreeObserver(); 
    getmeasure(vto,mytextview,scroll,linearLayout); 
} 



public void getmeasure(ViewTreeObserver vto, final TextView mytextview2, final ScrollView scroll2, final LinearLayout linearLayout2) { 


    vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 

     @Override 
     public void onGlobalLayout() { 
      int a=linearLayout2.getMeasuredHeight(); 
      int b=scroll2.getHeight(); 

      while (a<b) { 
       mytextview2.append("full full full"); 
       a=linearLayout2.getMeasuredHeight(); 
       b=scroll2.getHeight(); 
       } 

      } 
    }); 

} 

답변

0

getMeasuredHeight() 메서드는 onMeasure()에서 측정 된 heigth를 반환합니다. 문제는 onMeasure()가 Android 프레임 워크에서 호출되지 않았기 때문에 getMeasuredHeight()가 변경되지 않는다는 것입니다. 사실 while 루프는 프레임 워크가 뷰를 측정하지 못하게합니다.

이 같은 OnGlobalLayoutListener : 구현

텍스트가있는 LinearLayout하고 설계 및 부모 (있는 ScrollView)이 무효화 가야 후에 추가되어있어보기 다시 layouted한다
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 

    @Override 
    public void onGlobalLayout() { 
     int a=linearLayout2.getMeasuredHeight(); 
     int b=scroll2.getHeight(); 

     if (a<b) { 
      mytextview2.append("full full full");  
     } 

    } 
}); 

. 레이아웃은 뷰를 측정하는 것을 포함합니다. 즉, OnGlobalLayoutListener가 다시 호출됨을 의미합니다.

화면을 텍스트로 채울 수있는 좋은 방법은 아닙니다. 사실 TextView를 수직으로 스크롤 할 수 있도록하려면 ScrollView가 필요하지 않습니다. 그리고 그 내용이 화면보다 높게 보이지 않게하려면 왜 ScrollView가 필요한가요?

+0

"안드로이드 프레임 워크에서 onMeasure()가 호출되지 않았기 때문에 getMeasuredHeight()가 변경되지 않습니다."답변을 주셔서 감사합니다. "화면을 텍스트로 채우는 것은 좋지 않습니다."그런 식으로 조언 해주십시오. 분리 된 textView에 매우 긴 텍스트를 넣고 각 textView가 전체 화면을 차지하도록하십시오. (각 textView는 pageView에 있으며, 화면에 맞게 텍스트를 구분할 때를 알고 싶습니다.) 고마워요. –

+0

나는 그 문제를 안다는 쉬운 해결책이 없다. 그러나 Frameworks 레이아웃 프로세스를 반복해서 사용하여 적절한 양의 텍스트를 해결해서는 안됩니다. http://stackoverflow.com/questions/14276853/how-to-measure-textview-height-based-on-device-width-and-font-size의 답변에서와 같이 텍스트를 측정하고 올바른 내용을 검색 할 수 있습니다. 텍스트 양. 또는 TextView.getOffsetForPosition()을 사용하여 TextView에서 마지막 문자를 결정하고 다음 페이지에서 다음 문자로 시작하십시오. – thaussma

관련 문제