打破大文本到Android中的文字转换器或查看鳍页 [英] Breaking large text into pages in android text switcher or view flipper

查看:160
本文介绍了打破大文本到Android中的文字转换器或查看鳍页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在开发一个电子书阅读器应用程序的Andr​​oid 3.0平板电脑的过程。首先我有一个字符串数据一大块。我想分裂/打破字符串转换为基于设备的屏幕大小的页面[我打算使用文本转换器或查看鳍状肢。虽然我尝试使用getWindowManager()方法,我不能得到preferred结果。

在下面的线程中提到,文字切换器根据屏幕大小自动断开的文本。但是我不这么认为。 <一href="http://stackoverflow.com/questions/6406882/managing-text-in-android-applicaiton-like-in-a-ebook">Managing就像在一个电子书在Android的应用程序了文本

这是我用的逻辑:

 在检索//升降舵
    脚蹼=(ViewFlipper)findViewById(R.id.new_view_flipper);

    //获取屏幕尺寸
    显示显示= getWindowManager()getDefaultDisplay()。
    INT屏幕宽度= display.getWidth();
    INT screenHeight = display.getHeight();

    // contentString是本书的整个字符串

    而(contentString =空&安培;!&安培;!contentString.length()= 0)
    {
        总页数++;

        //为每一个页面的新textviews
        TextView的contentTextView =新的TextView(本);
        contentTextView.setWidth(ViewGroup.LayoutParams.FILL_PARENT);
        contentTextView.setHeight(ViewGroup.LayoutParams.FILL_PARENT);
        contentTextView.setMaxHeight(screenHeight);
        contentTextView.setMaxWidth(屏幕宽度);

        浮TEXTSIZE = contentTextView.getTextSize();
        涂料粉刷=新的油漆();
        paint.setTextSize(TEXTSIZE);

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

        而((lineCount&所述; maxLineCount)及及(numChars个&其中; contentString.length())){
            numChars个= numChars个+ paint.breakText(contentString.substring(numChars个),真实,屏幕宽度,NULL);
            lineCount ++;
        }

        //检索String要显示在当前文本
        串toBeDisplayed = contentString.substring(0,numChars个);
        contentString = contentString.substring(numChars个);
        contentTextView.setText(toBeDisplayed);
        flipper.addView(contentTextView);


        numChars个= 0;
        lineCount = 0;
    }
 

解决方案

这是所有你需要让你的code的工作。

  DisplayMetrics DM =新DisplayMetrics();
    。getWindowManager()getDefaultDisplay()getMetrics(DM)。
    INT屏幕宽度= dm.widthPixels;
    INT screenHeight = dm.heightPixels;
 

跟我的更换你下面code座。它将工作。

 显示显示= getWindowManager()getDefaultDisplay()。
INT屏幕宽度= display.getWidth();
INT screenHeight = display.getHeight();
 

I am in the process of developing an e-book reader application for android 3.0 tablets. To begin with I have a large chunk of String data. I want to split/break that String into pages based on the screen size of the device [I am planning to use text switcher or view flipper]. Though I tried using getWindowManager() method I could not get the preferred results.

In the following thread it is mentioned that Text Switcher automatically breaks the text according to the screen size. But I do not think so. Managing text in android applicaiton like in a eBook

This is the logic I used :

    // 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;
    }

解决方案

This is all you need to make your code work.

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

Replace your following code block with mine. It will work.

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

这篇关于打破大文本到Android中的文字转换器或查看鳍页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆