将长字符串分成多个页面

时间:2013-01-06 21:10:53

标签: android

我正在尝试将长字符串显示在多个页面中。我的Activity.xml有一个textview占据了屏幕高度的90%(这是我设置了relativelayout)。我需要一些帮助才能找到可以用来实现我的目标的正确方法/类

以下是我的代码。粗体内容(代码中以**开头的注释)是我需要帮助的地方。

public class StoryActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_story);

    String contentString;
    String pageString;
    final int screenWidth;
    final int screenHeight;
   final int pageWidth;
   final int pageHeight;
   final int totalPages;
   int pageNumber;


    //load the content into the string contentString    
    InputStream is = getAssets().open("Story.txt");
        int size = is.available();
       byte[] buffer = new byte[size];
       is.read(buffer);
       is.close();
     contentString = new String(buffer);

           // get screen dimensions       
       DisplayMetrics dm = new DisplayMetrics();
       getWindowManager().getDefaultDisplay().getMetrics(dm);
       screenWidth = dm.widthPixels;
       screenHeight= dm.heightPixels;

       // create textview
       TextView tv = (TextView)findViewById(R.id.textViewStory);

             // calculate pageheight since textview size is 90% of the screen 
            pageHeight = (int) (0.9* screenHeight);
            pageWidth = screenWidth;

            **// 1)need to find number of lines which will fit pageHeight
            // 2)then I need to find total number of lines in my String contentString
            // 3)dividing 2) by 1) can give me number of pages required to display 
            // contentString. 
            // 4)I can then concatenate contentString at number of lines as found in 
            // 1) and thus I know what text (pageString) to display in page1**

            tv.setText(pageString); 

            **// steps 4) can be repeated to display next page when user click 
            // “nextpage” button**         

    }

1 个答案:

答案 0 :(得分:2)

我不会为您提供完整的解决方案,但我找到了可以帮助您的答案。

  1. First有助于查找TextView宽度中有多少个字符。
  2. Second有助于了解TextView准确显示的行数。
  3. 但换行存在问题。您需要应用一些算法来找出android框架如何包装文本或为此执行此操作。以下是文本换行的good answer

相关问题