我正在为 android 3.0 平板电脑开发电子书阅读器应用程序。首先,我有一大块字符串数据。我想根据设备的屏幕大小将该字符串拆分/拆分为页面 [我打算使用文本切换器或 View 翻转器]。虽然我尝试使用 getWindowManager() 方法,但我无法获得首选结果。
在以下线程中提到文本切换器会根据屏幕大小自动拆分文本。但是我不这么认为。 Managing text in android applicaiton like in a eBook
这是我使用的逻辑:
// 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;
}
最佳答案
这就是让您的代码正常工作所需的一切。
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int screenWidth = dm.widthPixels;
int screenHeight= dm.heightPixels;
用我的替换下面的代码块。它会起作用。
Display display = getWindowManager().getDefaultDisplay();
int screenWidth = display.getWidth();
int screenHeight = display.getHeight();
关于android - 在 android 文本切换器或 View 翻转器中将大文本分成页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7225994/