2017-06-20 70 views
4

我正在嘗試構建一本書閱讀器。而現在我有一個很長的文本,填滿了整個屏幕,甚至一些文本沒有了屏幕。我想知道如何找到在屏幕上完全可見的最後一個字母,這樣我就可以將文本分成頁面。Android:如何確定TextView中最後一個完全可見的字母?

例如,所有的行之前

「壞消息,弗農,」她說......

完全可見的文本,其餘文本必須移動到另一頁。

enter image description here

這樣的(另一個文本頁面與 「壞消息」 開始)

enter image description here

這裏是我以前在例如

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/activity_main" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:padding="10dp"> 

<TextView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:text="...some text..." /> 
</RelativeLayout> 
+1

你正在努力實現頁面夾板 請檢查了這一點 https://stackoverflow.com/questions/20204348/how-to-break-styled-text-into-pages-in-android – argando

+0

我曾嘗試示例應用程序從argando的鏈接提供,它似乎工作爲我想要 – Season

回答

1

我上面的佈局認爲預測文本將要結束的地方將是困難/不可能的,除非你使用了等寬字體,例如信使。但是不要採取這種方法,我可以建議的另一種方法是簡單地爲圖書閱讀器的每個頁面提供不變數量的字符。是的,這意味着如果使用非等寬字體,則不會知道要呈現的確切行數。但是,佔用的線數應該在相當窄的範圍內,並且很可能不會被用戶察覺。

您可以在這裏使用ScrollView,其中包含實際的TextView,其中包含每個頁面的文本。這裏是什麼,可能看起來像一個骨架:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/activity_main" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:padding="10dp"> 

    <ScrollView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"> 
     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:text="...some text..." /> 
    </ScrollView> 
</RelativeLayout> 

使用ScrollView,用戶可以然後只需上下拖動趕上這可能不適合到單個屏幕的任何內容。再次,這使您不必擔心提供確切數量的內容。

+0

我知道使用ScrollView與TextView是一種簡單的方法來顯示長文本,但它仍然不是一個分頁方法 – Season

+0

@Season我說的是使用一個滾動視圖,然後你不必擔心「頁面分割」。大多數讀者可能會遵循這種方法 –

相關問題