在Android文本切换器或视图翻转器中分页大文本

7
我正在开发一款适用于Android 3.0平板电脑的电子书阅读器应用程序。首先,我有一大块字符串数据。我想根据设备的屏幕大小(我打算使用文本切换器或视图翻转器)将该字符串分割成页面。尽管我尝试使用getWindowManager()方法,但我无法获得所需的结果。
在下面的线程中提到了Text Switcher会根据屏幕大小自动分页。但我并不这么认为。 管理Android应用程序中的文本,如同电子书 以下是我使用的逻辑:
    // 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;
    }

请看我的答案,链接在这里:https://dev59.com/7nnZa4cB1Zd3GeqPrY7u - mixel
1个回答

2
这是让你的代码正常工作所需的全部内容。
    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();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接