我正在开发图书阅读器应用程序。我有LinearLayout。
<LinearLayout
android:id="@+id/llReader"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:orientation="vertical" >
</LinearLayout>
我正在逐行从内部存储中获取html文件。我正在为一行分配一个文本视图并将它们放入LinearLayout。
private void readFile(LinearLayout ll) throws IOException {
fileName = myDatabase.getBookById(book_id) + ".html";
FileInputStream fis = openFileInput(fileName);
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String line = "";
while (null != (line = br.readLine())) {
TextView tv = new TextView(this);
tv.setTextSize(24);
tv.setText(Html.fromHtml(line));
ll.addView(tv);
}
br.close();
}
如何识别TextViews到达屏幕底部?
答案 0 :(得分:1)
您可以通过像素操作:
private void readFile(LinearLayout ll) throws IOException {
fileName = myDatabase.getBookById(book_id) + ".html";
FileInputStream fis = openFileInput(fileName);
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String line = "";
int parentHeight = ll.getHeight(); // in pixels
int sumHeigth = 0;
while (null != (line = br.readLine())) {
TextView tv = new TextView(this);
tv.setTextSize(24);
tv.setText(Html.fromHtml(line));
ll.addView(tv);
sumHeigth += tv.getHeight();
if(sumHeigth>parentHeight) {
// if can't fit in LinearLayout
ll.removeView(tv);
// break; // or what you want in this situation
}
}
br.close();
}