跨使用ViewPager多个TextViews /片段分割文本 [英] Splitting text across multiple TextViews/Fragments using ViewPager

查看:175
本文介绍了跨使用ViewPager多个TextViews /片段分割文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想分割为装入ViewPager多个片段一些文字使用Android兼容性库。这个想法是有一个类似于Kindle的应用程序,你在哪里刷卡跨多个页面的文本。

起初,我的文字的来源是strings.xml中定义的字符串,我使用的算法来确定设备屏幕的高度,然后相应的分割这个字符串到数字的ViewPager项目。正如你不能提前一个TextView的高度,我只是用一个百分比值来确定的TextView中的最大大小设置为每个页面上(这似乎有点难看)。

这看起来像一个合理的做法,还是我这一切的错误的方式?

解决方案
  

正如你不能让一个TextView的高度提前

嗯,这取决于你所说的提前的意思(时间==文本处理?)。

  • 您可以措施() ViewPager / 的TextView 一次,并使用其 getMeasuredHeight()/ getMeasuredWidth()在计算中
  • 您可以在 ViewTreeObserver.OnGlobalLayoutListener.onGlobalLayout等待(),并使用标准布局三围为同样的事情
  • (不推荐),可以使用(屏幕高度 - 装饰品高度)/文字高度(字体+间隔)知道有多少文本行会适合你的TextView
  • 等。等等。

I'm trying to split some text across multiple Fragments loaded into a ViewPager using the Android compatibility library. The idea is to have something akin to the Kindle app, where you are swiping across multiple pages of text.

Initially, my source of text is a string defined in strings.xml and I am using an algorithm to determine the height of the device screen, and then split this string accordingly into a number of items for the ViewPager. As you can't get the height of a TextView ahead of time, I am just using a percentage value to determine what to set the maximum size of the TextView to on each page (this seems a bit ugly).

Does this seem like a reasonable approach, or am I going about this all the wrong way?

解决方案

As you can't get the height of a TextView ahead of time

Well, that depends on what you mean by "ahead of time" (time == text processing?).

  • You can measure() your ViewPager/TextView once and use its getMeasuredHeight()/getMeasuredWidth() in your calculations
  • You can wait for ViewTreeObserver.OnGlobalLayoutListener.onGlobalLayout() and use the "standard" layout measurements for the very same thing
  • (Not recommended) you can use ("screen height" - "decorations height") / "text height (font + spacing)" to know how many text lines will fit in your TextView
  • Etc. etc.

这篇关于跨使用ViewPager多个TextViews /片段分割文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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