Je suis en train de développer une application lecteur de livre électronique pour les tablettes Android 3.0. Pour commencer, j'ai un gros morceau de données String. Je veux diviser/casser cette chaîne dans les pages basées sur la taille de l'écran de l'appareil [Je prévois d'utiliser le sélecteur de texte ou flipper vue]. Bien que j'aie essayé d'utiliser la méthode getWindowManager(), je n'ai pas pu obtenir les résultats souhaités.Rupture de texte volumineux dans les pages dans le sélecteur de texte android ou flipper vue
Dans le fil suivant, il est mentionné que Text Switcher casse automatiquement le texte en fonction de la taille de l'écran. Mais je ne le pense pas. Managing text in android applicaiton like in a eBook
Telle est la logique je:
// 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;
}
Voir ma réponse ici http://stackoverflow.com/questions/20204348/how-to-break-styled-text-into-pages-in-android – mixel