2011-08-29 13 views
7

Jestem w trakcie opracowywania aplikacji czytnika e-booków dla tabletów z systemem Android 3.0. Na początek mam duży fragment danych String. Chcę podzielić/podzielić ten ciąg na strony na podstawie rozmiaru ekranu urządzenia [planuję użyć przełącznika tekstu lub widoku flipper]. Chociaż próbowałem użyć metody getWindowManager(), nie mogłem uzyskać preferowanych wyników.Przełamywanie dużego tekstu na strony w przełączniku tekstowym z Androidem lub widoku flippera

W następującym wątku wspomina się, że Text Switcher automatycznie łamie tekst zgodnie z rozmiarem ekranu. Ale nie sądzę. Managing text in android applicaiton like in a eBook

Taka jest logika użyłem:

// retreiving the flipper  
    flipper = (ViewFlipper) findViewById(R.id.new_view_flipper);   

    // obtaining screen dimensions  
    Display display = getWindowManager().getDefaultDisplay(); 
    int screenWidth = display.getWidth(); 
    int screenHeight = display.getHeight(); 

    // contentString is the whole string of the book 

    while (contentString != null && contentString.length() != 0) 
    { 
     totalPages ++; 

     // creating new textviews for every page 
     TextView contentTextView = new TextView(this); 
     contentTextView.setWidth(ViewGroup.LayoutParams.FILL_PARENT); 
     contentTextView.setHeight(ViewGroup.LayoutParams.FILL_PARENT); 
     contentTextView.setMaxHeight(screenHeight); 
     contentTextView.setMaxWidth(screenWidth); 

     float textSize = contentTextView.getTextSize(); 
     Paint paint = new Paint(); 
     paint.setTextSize(textSize); 

     int numChars = 0; 
     int lineCount = 0; 
     int maxLineCount = screenHeight/contentTextView.getLineHeight(); 
     contentTextView.setLines(maxLineCount); 

     while ((lineCount < maxLineCount) && (numChars < contentString.length())) { 
      numChars = numChars + paint.breakText(contentString.substring(numChars), true, screenWidth, null); 
      lineCount ++; 
     } 

     // retrieve the String to be displayed in the current textbox 
     String toBeDisplayed = contentString.substring(0, numChars); 
     contentString = contentString.substring(numChars); 
     contentTextView.setText(toBeDisplayed); 
     flipper.addView(contentTextView); 


     numChars = 0; 
     lineCount = 0; 
    } 
+0

Zobacz moją odpowiedź tutaj http://stackoverflow.com/questions/20204348/how-to-break-styled-text-into-pages-in-android – mixel

Odpowiedz

2

To jest wszystko, co trzeba zrobić swoją pracę kodu.

DisplayMetrics dm = new DisplayMetrics(); 
    getWindowManager().getDefaultDisplay().getMetrics(dm); 
    int screenWidth = dm.widthPixels; 
    int screenHeight= dm.heightPixels; 

Zamień poniższy blok kodu na mój. To będzie działać.

Display display = getWindowManager().getDefaultDisplay(); 
int screenWidth = display.getWidth(); 
int screenHeight = display.getHeight();