2011-08-29 5 views
7

私はアンドロイド3.0タブレット用の電子ブックリーダーアプリケーションを開発中です。まず、Stringデータの大きな部分があります。私は、デバイスの画面サイズに基づいてページに[ストリングを分割/分割したい] [私はテキストスイッチャーまたはビューフリッパーを使用する予定です]。私は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; 
    } 
+0

ここに私の答えを見なさいhttp://stackoverflow.com/questions/20204348/how-to-break-styled-text-int-pages-in-android – mixel

答えて

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(); 
関連する問題