下面列出了android.widget.TextView#scrollTo ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void onTakeFocus(TextView widget, Spannable text, int dir) {
Layout layout = widget.getLayout();
if (layout != null && (dir & View.FOCUS_FORWARD) != 0) {
widget.scrollTo(widget.getScrollX(),
layout.getLineTop(0));
}
if (layout != null && (dir & View.FOCUS_BACKWARD) != 0) {
int padding = widget.getTotalPaddingTop() +
widget.getTotalPaddingBottom();
int line = layout.getLineCount() - 1;
widget.scrollTo(widget.getScrollX(),
layout.getLineTop(line+1) -
(widget.getHeight() - padding));
}
}
/**
* Performs a scroll left action.
* Scrolls left by the specified number of characters.
*
* @param widget The text view.
* @param buffer The text buffer.
* @param amount The number of characters to scroll by. Must be at least 1.
* @return True if the event was handled.
* @hide
*/
protected boolean scrollLeft(TextView widget, Spannable buffer, int amount) {
final int minScrollX = getScrollBoundsLeft(widget);
int scrollX = widget.getScrollX();
if (scrollX > minScrollX) {
scrollX = Math.max(scrollX - getCharacterWidth(widget) * amount, minScrollX);
widget.scrollTo(scrollX, widget.getScrollY());
return true;
}
return false;
}
/**
* Performs a scroll right action.
* Scrolls right by the specified number of characters.
*
* @param widget The text view.
* @param buffer The text buffer.
* @param amount The number of characters to scroll by. Must be at least 1.
* @return True if the event was handled.
* @hide
*/
protected boolean scrollRight(TextView widget, Spannable buffer, int amount) {
final int maxScrollX = getScrollBoundsRight(widget) - getInnerWidth(widget);
int scrollX = widget.getScrollX();
if (scrollX < maxScrollX) {
scrollX = Math.min(scrollX + getCharacterWidth(widget) * amount, maxScrollX);
widget.scrollTo(scrollX, widget.getScrollY());
return true;
}
return false;
}
/**
* Performs a scroll to line start action.
* Scrolls to the start of the line.
*
* @param widget The text view.
* @param buffer The text buffer.
* @return True if the event was handled.
* @hide
*/
protected boolean scrollLineStart(TextView widget, Spannable buffer) {
final int minScrollX = getScrollBoundsLeft(widget);
int scrollX = widget.getScrollX();
if (scrollX > minScrollX) {
widget.scrollTo(minScrollX, widget.getScrollY());
return true;
}
return false;
}
/**
* Performs a scroll to line end action.
* Scrolls to the end of the line.
*
* @param widget The text view.
* @param buffer The text buffer.
* @return True if the event was handled.
* @hide
*/
protected boolean scrollLineEnd(TextView widget, Spannable buffer) {
final int maxScrollX = getScrollBoundsRight(widget) - getInnerWidth(widget);
int scrollX = widget.getScrollX();
if (scrollX < maxScrollX) {
widget.scrollTo(maxScrollX, widget.getScrollY());
return true;
}
return false;
}
private void addText(TextView textView, String content) {
textView.append(content);
textView.append("\n");
int offset = textView.getLineCount() * textView.getLineHeight();
if (offset > textView.getHeight()) {
textView.scrollTo(0, offset - textView.getHeight());
}
}
/**
* Scrolls the specified widget to the specified coordinates, except
* constrains the X scrolling position to the horizontal regions of
* the text that will be visible after scrolling to the specified
* Y position.
*/
public static void scrollTo(TextView widget, Layout layout, int x, int y) {
final int horizontalPadding = widget.getTotalPaddingLeft() + widget.getTotalPaddingRight();
final int availableWidth = widget.getWidth() - horizontalPadding;
final int top = layout.getLineForVertical(y);
Alignment a = layout.getParagraphAlignment(top);
boolean ltr = layout.getParagraphDirection(top) > 0;
int left, right;
if (widget.getHorizontallyScrolling()) {
final int verticalPadding = widget.getTotalPaddingTop() + widget.getTotalPaddingBottom();
final int bottom = layout.getLineForVertical(y + widget.getHeight() - verticalPadding);
left = Integer.MAX_VALUE;
right = 0;
for (int i = top; i <= bottom; i++) {
left = (int) Math.min(left, layout.getLineLeft(i));
right = (int) Math.max(right, layout.getLineRight(i));
}
} else {
left = 0;
right = availableWidth;
}
final int actualWidth = right - left;
if (actualWidth < availableWidth) {
if (a == Alignment.ALIGN_CENTER) {
x = left - ((availableWidth - actualWidth) / 2);
} else if ((ltr && (a == Alignment.ALIGN_OPPOSITE)) ||
(!ltr && (a == Alignment.ALIGN_NORMAL)) ||
(a == Alignment.ALIGN_RIGHT)) {
// align_opposite does NOT mean align_right, we need the paragraph
// direction to resolve it to left or right
x = left - (availableWidth - actualWidth);
} else {
x = left;
}
} else {
x = Math.min(x, right - availableWidth);
x = Math.max(x, left);
}
widget.scrollTo(x, y);
}